I have a CSV file that I receive daily that I format using a macro that is stored in my personal macro file. I am trying to create a vbs file that will run the macro on the CSV file via a scheduled task, but I am not having an luck. Here is the vbs file:
Option Explicit
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("c:\users\dmcgettigan\desktop\test.csv", 0, True)
xlApp.Run "c:\users\dmcgettigan\desktop\Personal.xlsb!newsales"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
This is the error that I am getting:
C:\Users\dmcgettigan\Desktop\test.vbs(13, 3) Microsoft Excel: Cannot run the macro 'c:\users\dmcgettigan\desktop\Personal.xlsb!new sales'. The macro may not be available in this workbook or all macros may be disabled.
Any ideas on what I am doing wrong?
For me it's working like that
Dim xlApp
Set xlApp = CreateObject("Excel.Application")
xlapp.workbooks.open("<Path to your PERSONAL.XLSB>")
xlApp.Run "PERSONAL.XLSB!Test"
xlApp.Quit
The comment is also right, you can't have a blank in the name of the sub you want to run.
Update Without being able to test it your code should look like this
Option Explicit
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("c:\users\dmcgettigan\desktop\test.csv", 0, True)
xlapp.workbooks.open("c:\users\dmcgettigan\desktop\Personal.xlsb")
xlApp.Run "Personal.xlsb!newsales"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Related
After an current update of Excel/Office365 to version 16.03.13127.21064 (64bit) the below dialog (Others are also making changes) pops up if I open a file with VBScript.
I already use Application.DisplayAlers = False in my VBScript to suppress dialogues, but for this dialog the setting has no effect.
This is my script:
Option Explicit
Dim inputFile
Dim macroName
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
xlApp.Visible = False
xlApp.EnableEvents = False
Set xlBook = xlApp.Workbooks.Open(inputFile)
xlApp.Run macroName
'...
Is there another option to switch Excel in a kind of interactive mode.
Thank you for help
Andreas
I'm currently trying to make a macro that opens a user defined excel spreadsheet, extracts some data for use in the word document and then closes it. My problem is that when I run the macro, the spreadsheet that I opened is still technically open as a background process in my task manager. I read on another stack overflow question that the reason is because visual basic will not release the reference object from excel until I close out of Microsoft Word. However, even after closing out of Word, the excel background process is still going and I can only stop it by ending the task in the task manager. To clarify, if I run the macro, close Word and then try to open the excel file, I can get in without telling me it's a read only file. However, if I don't close out of Word and I try to go into the spreadsheet after running the macro, then it tells me that it's a read only file. Below is the code I'm using that is causing this problem for me. Thanks to anyone who can help.
Sub UpdateProposal()
'Declares variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim SpreadsheetPath As String
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim ProposalInfoArr(1 To 30) As String
'Skips to ErrorHandler if user cancels out of file dialog
On Error GoTo ErrorHandler
'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in SpreadsheetPath variable
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Stores in SpreadsheetPath variable
SpreadsheetPath = .SelectedItems.Item(1)
End With
'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set xlApp = New Excel.Application
End If
'If you want Excel to be visible, you could add the line: xlApp.Visible = True here; but your code will run faster if you don't make it visible
'Open the workbook
Set xlBook = xlApp.Workbooks.Open(FileName:=SpreadsheetPath)
'''Extracts Data
'Quits out of Excel if it was not running previous to running the macro.
If ExcelWasNotRunning Then
xlApp.DisplayAlerts = False
xlApp.Quit
End If
'Make sure you release object references.
Set xlRange = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
'Ends the macro before the error handler
Exit Sub
'Ends Macro
ErrorHandler:
MsgBox "The following error occurred: " & Err.Description
End Sub
You are defining the objects correctly:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
but you forgot about the implicitly used Workbooks object... as most of the answers you will find do... which means it doesn't get released. So do it like this:
Dim SpreadsheetPath As String
Dim xlApp As Excel.Application
Dim xlBooks As Excel.Workbooks
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
SpreadsheetPath = "C:\MyPath\MyFile.xlsx"
Set xlApp = New Excel.Application
' Set xlApp = GetObject(, "Excel.Application") ' or attach to an existing one
Set xlBooks = xlApp.Workbooks
Set xlBook = xlBooks.Open(FileName:=SpreadsheetPath) ' you can use .Add instead if someone else may have it open already
Set xlSheet = xlBook.Worksheets(1)
Set xlRange = xlSheet.Range("A1")
' do stuff with the worksheet/range
xlRange.Value = "foo"
' the order matters
' just like it does
' when you create the objects
Set xlRange = Nothing
Set xlSheet = Nothing
xlBook.Close False
Set xlBook = Nothing
Set xlBooks = Nothing
xlApp.Quit
Set xlApp = Nothing
However, you may find that it still isn't getting released when you want, but it will get released when you close the program you are using to create it (in your case, MS-Word) as that is (presumably) when Windows does its built-in garbage collection.
Note: I removed the error handling just to keep it a clean example, but you can leave that in
I have a .vba file located in a network folder let's call it helloworld.vba inside this file I have a sub function HelloFromTheOtherSide().
I'm trying to programmatically load this file in a way that I can execute the functions HelloFromTheOtherSide() in my excel instance.
Imagine that this is the content for simplicity:
Sub HelloFromTheOtherSide()
MsgBox ("hello there!")
End Sub
I've tried to follow these instructions on how to dynamically add and run a VBA macro from Visual Basic but that is not what I'm trying to do here as I want to be able to run calling HelloFromTheOtherSide().
I'm trying to understand if its possible to load a .vba from a folder to an excel instance programmatically in a way I could run the functions in my instance.
That would be quite usefull if possible as I would be able to store all my vba code in a single folder and load it from there everytime that I want to run something specific.
You can use both VBA or VBScript to call a Function or a Sub from another Workbook:
VBA
Sub callExternalFunction()
Dim xlApp As Object
Dim xlBook As Object
'Define Excel App
Set xlApp = CreateObject("Excel.Application")
'Open Workbook
Set xlBook = xlApp.Workbooks.Open("\\server\my\path\to\My library.xlsm", 0, True)
'Call my Sub, with eventual parameters (if you don't have any, just call the Routine)
xlApp.Run "Myfunction", "param_1", "param_2"
'Quit and clean
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
VBScript
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
'Define Excel App
Set xlApp = CreateObject("Excel.Application")
'Open Workbook
Set xlBook = xlApp.Workbooks.Open("R:\my\network\path\My Workbook.xlsm", 0, True)
'Call my Sub, with eventual parameters (if you don't have any, just call the Routine)
xlApp.Run "myRoutine"
'Quit and clean
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Edit
You can omit the Excel App initialization and directly call the Macro you need with this command (thanks to #TimWilliams):
Application.Run "'MyBook.xls'!MyMacroName"
Note: As you can see they are pretty similar. Both codes are tested and working on my Excel.
Hope this helps.
I want to call a macro from Outlook.
I can do it with an Excel .xlsm file.
How can I call a macro in an addin .xlam file?
Example for .xlsm
Sub trans_outlook()
Dim xlApp As Object, xlWkb As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False ' can be False if you do not wont see reaction, byt make sure is not fail
Set xlWkb = xlApp.Workbooks.Open("C:\Users\xyz.xlsm")
xlApp.Run "xyz.xlsm!Test"
xlWkb.Close savechanges:=False
xlApp.Quit
End Sub
You need to go to Tools -> References. Find Microsoft Excel 14.0 Data Objects Library in the list, and check the box next to it. Click OK
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = New Excel.Application
Set ExWbk = ExApp.Workbooks.Open("C:\Folder\Folder\File.xls")
ExApp.Visible = True
ExWbk.Application.Run "ModuleName.YourMacro"
ExWbk.Close SaveChanges:=True
I have a VBScript code to open an excel file, run a macro and close it. Fine.
Now, the only thing I want to change is to leave the file open.
If I remove the 2 lines of code xlApp.activewindow.close and xlApp.Quit, then the workbook and the application are closed anyway, but they remain open in the background (Excel process still active in Task Manager). Hence, it is impossible to re-run the macro later on the same file by calling the script again (which is exactly what I want to do).
Why?
Here is the code:
Option Explicit
On Error Resume Next
MyTest
Sub MyTest()
Dim xlApp
Dim xlBook
Dim fpath
Dim fname
' Excel application running? if not, open Excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp <> "Microsoft Excel" Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
' correct Excel file open? if not, open it
fpath = "D:\Desktop\"
fname = "MyTest.xls"
xlApp.Workbooks(fname).Activate
If Err = 0 Then
' no error, so it has been possible to activate the workbook
Set xlBook = xlApp.Workbooks(fname)
Else
' unable to activate, so workbook was not open -> open it now
Set xlBook = xlApp.Workbooks.Open(fpath & fname, 0, True)
End If
Err.Clear
' now run the desired macro in the excel file
xlApp.Run "HelloWorld"
' WANT TO CHANGE THIS
xlBook.saved = True
xlApp.activewindow.close
' AND THIS
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
You just need to make your new instance of Excel visible. Do this right after creating it:
xlApp.Visible = True
This line of code will close your current activated workbook (by now, it is D:\Destop\MyTest.xls);
xlApp.activewindow.close
This line will quit Excel application;
xlApp.Quit