I have a vba macro to open excels and read data.
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
Set excelworkbook = xlApp.Workbooks.Open(file_path, ReadOnly:=True, notify:=False)
And error "Too many different cell formats" occurred on the third line. I assume it's because of cell formats(fonts, borders, etc.) and does anyone know how to resolve this issue programmatically? No error is raised when I try to open the excel manually. Any advice would be appreciated.
Instead of your code, try doing it this way:
Dim excelWorkbook As Excel.Workbook
Set excelWorkbook = Excel.Workbooks.Open(file_path, ReadOnly:=True, Notify:=False)
If it still gives you trouble, maybe adding these will help:
Application.DisplayAlerts = False
' Application.ScreenUpdating = False ' Optional, might not be necessary.
Just remember to set them back to their defaults afterwards.
Related
I have a macro in my Outlook that whenever I receive an e-mail with a certain subject, it automatically opens an Excel workbook and pastes a portion of the e-mail subject in a specific cell in one of the worksheets. It works perfectly. Now I need to do this exact same process but pasting this information in an already opened workbook, instead of opening a closed file.
I've tried different solutions from my limited Excel VBA knowledge (ActiveWorkbook, worbooks(filename).activate, etc.) but none of that worked and I have not found anything similar online, as most macros are written as being run from an Excel file and not Outlook.
This is part of our current code, that opens the file and pastes the e-mail subject (which is the "ticker" value) in a specific cell on the "Lista Empresas" worksheet. What I need is a code that does the same, but in an workbook that is already opened (let's call it "test1").
Dim exapp As Excel.Application
Dim ExWbk As Workbook
Dim ExWbk2 As Workbook
Set exapp = New Excel.Application
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
exapp.Visible = True
ExWbk2.Sheets("Lista Empresas").Range("P2").Value = ticker
ExWbk2.Sheets("Lista Empresas").Range("P3").Calculate
There are a few scenarios to handle here. First, is Excel running? If no, then do what you are doing already. If yes - is the correct workbook open? If yes - return it, otherwise open it.
Dim ExWbk2 As Workbook
Dim wb As Workbook
Dim exapp As Excel.Application
On Error Resume Next
Set exapp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set exapp = Nothing
End If
On Error GoTo 0
If exapp Is Nothing Then
' Excel is not running
Set exapp = New Excel.Application
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
ExWbk2.Visible = True
Else
' Excel is running, but is the right book open?
For Each wb In exapp.Workbooks
Debug.Print wb.Name ' <-- This will help you find what to look for
If wb.Name = "ListaEmpresas_ajustado" Then
' Yes, it is!
Set ExWbk2 = wb
End If
Next
If ExWbk2 Is Nothing Then
' No, it wasn't
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
End If
End If
The trick to find out if Excel is running is GetObject. It will fail if it can't find it.
The for loop is there to allow for finding the correct workbook, based on the name. Adjust as needed.
The following code gets the object if you know the name of the sheet currently active in Excel instance. I guess this could be got from the application title using the first bit of code.
Dim exapp As Excel.Application
Dim ExWbk As Workbook
Dim ExWbk2 As Workbook
Set exapp = GetObject("ListaEmpresas_ajustado.xlsm").Application
exapp.Visible = True
ExWbk2.Sheets("Lista Empresas").Range("P2").Value = ticker
ExWbk2.Sheets("Lista Empresas").Range("P3").Calculate
I am trying to fix some code, but have no experience with the Microsoft Visual Basic. When closing an active workbook I am getting the runtime error '91', the code which is causing the error is:
Set appExcel = New Excel.Application
appExcel.Visible = True
' More VBA code here, I can post if it would be necessary but I would
' assume this is enough code to see the problem
appExcel.ActiveWorkbook.Close
appExcel.Quit
I am assuming that at this point the Excel active workbook would close and don't know why this code isn't doing this
You are getting that error because there is no Workbooks open in that instance at that moment.
If you try this code, you will see that you will not get an error
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = True
'
'~~> Rest of the code
'
appExcel.Workbooks.Add '<~~ Add a temp workbook
appExcel.ActiveWorkbook.Close
To handle that use this
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = True
'
'~~> Rest of the code
'
If appExcel.Workbooks.Count > 0 Then appExcel.ActiveWorkbook.Close
Best is to work with Objects so that you do not close the wrong workbook by mistake. For example
Dim wb As Workbook
Set wb = appExcel.Workbooks.Add
'
'~~> Rest of the code
'
If Not wb Is Nothing Then wb.Close
Set wb = Nothing
I am opening an excel workbook from excel and delete a sheet inside. I want to do this without the message from Excel:
"Excel will permanently delete this sheet, do you want to continue"
However I cannot make the "DisplayAlerts = False" work correctly.
Public Sub xportQuery()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim PathDaily, FileName As String
PathDaily = Forms!Menu!Text69
FileName = Forms!Menu!Text84
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open(PathDaily & FileName)
appExcel.Visible = True
'Set appExcel = Nothing
Set myWorkbook = Nothing
appExcel.DisplayAlerts = False
Workbooks(FileName).Sheets("Sheety").Delete
appExcel.DisplayAlerts = True
End Sub
Writing it this way I get a "Subscript out of range" on the Sheets("Sheety").delete
If I take of the two DisplayAlerts lines, the code works but with the alert.
How do I write my code correctly to work without any alert message?
Note: DoCmd.SetWarnings didn t work either as the message is displayed in Excel
Thanks To Sam's Comment:
it works either by changing
Workbooks(FileName).Sheets("Sheety").Delete
into
myWorkbook.Sheets("Sheety").Delete
or
AppExcel.Workbooks(FileName).Sheets("Sheety").Delete
However the rest of the macro can still use Workbooks(Filename) without the "AppExcel." Reference
I am trying to have a vba macro in PowerPoint to open and retrieve information from an Excel file.
The Excel file is saved in a SharedPoint and I am able to open it. However, when I want to refer to the Workbook as to retrieve information from it's cells, it gives me the following error: Automation error invalid syntax -2147221020 (800401e4)
Here is the code I have so far:
Sub test()
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Application.DisplayAlerts = False
'open excel from sharedpoint
ActivePresentation.FollowHyperlink _
Address:="http://teams.blabla.com/Shared%20Documents/Overview_NPD_Projects.xlsm", _
NewWindow:=True, AddHistory:=True
Set wb = GetObject("http://teams.blabla.com/Shared Documents/Overview_NPD_Projects.xlsm")
' this does not work even when I change it to "Shared%20Documents"
Set ws = wb.Worksheets("Project List")
auxvariable = ws.Range("B2").Value
End Sub
The funny thing is that it works if I try to save the excel file manually to my desktop before running the macro and change the the path to:
Set wb = GetObject("C:\Users\Filipe.freitas\Desktop\Overview_NPD_Projects.xlsm")
I would save the file to the desktop and use the correct path, but I would need to refer to the excel file before doing it.
Any help would be greatly appreciated!
Have you tried http://www.cpearson.com/Excel/DownloadFile.aspx and then
Dim xlapp as object
xlapp = createobject("Excel.Application")
set wb = xlapp.workbooks.open("thefilenamewhereyousavethedownload")
I'm new to VBA, so I'm struggling with this for a couple of days now.
I have a combobox in Word with contacts, I also have an excel file(contacts.xls) with one column (names of the all the contacts). When I open the Word-document the Macro has to fill in the combobox with all the names from the excel file.
Is it possible to send me a working example of this for word 2007? Seems quite simple, but just can't get this to work...
Thanks in advance!
If you intend on reading from an Excel file in Word you are going to have to open it. It will use code like this:
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open("FileName.xlsx")
You will also probably want to go to Tools->References in the VB project and find whatever Excel library is on your machine (and check it of course). There are ways around this if needed, but it is easier.
You then can read the values from the workbook:
oBook.Worksheets(1).cells(1,1)
I'm not totally sure what the syntax to get it into the combo box is. Look up "combobox object members" in the vbe in word. There will be a list property, or something like that.
Sorry, I'm on a linux machine right now, so I can't debug exact code for you.
I have more full code for you now:
Option Explicit
Sub TestDropDownFromExcel()
Dim counter As Long
Dim xlApp As Excel.Application
Dim xlBook As Workbook
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls(1)
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Path\To\MyFile.xlsx")
If xlBook Is Nothing Then
Exit Sub
End If
oCC.DropdownListEntries.Clear
For counter = 1 To 10
oCC.DropdownListEntries.Add Text:=xlBook.Worksheets(1).Cells(counter, 1), Value:=CStr(counter)
Next counter
xlApp.Visible = True
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Be aware that there is very little error checking going on here. Also, rather than the call to .Visible at the end, you can simply call .Close if you do not want the user to see it (for the final project, this is probably preferable. The deferencing (= Nothing) is good practice to have, but VBA cleans up automatically at the end of execution. Obviously I am assuming tyou want the first dropdown (ContentCOntrols(1)) but this may not be true.