I am trying to replace on the line Application.Run "c:\users\navin\test\" with path but it won't work. (Error 1004).
Sub test()
Dim path As String
path = "c:\users\navin\test\"
Workbooks.Open (path & "excel.xlsb")
Application.Run "'c:\users\navin\test\new.xlsb!macro1'"
Workbooks("excel.xlsb").Close SaveChanges:=True
End Sub
Currently you open a workbook called excel.xlsb and then attempt to run a macro in a workbook called new.xslb. You also have two sets of quotes, which is likely to cause problems.
Where you attempt to run the macro, you should reference only the name of the workbook.
Application.Run "excel.xlsb!macro1"
Related
I have a workbook which opens up another workbook (filename is based on a cell value) and then runs a macro called Single_sector within that file.
It opens the file perfectly fine but doesn't run the macro. Any ideas?
Sub run_all()
Dim Location
On Error Resume Next
'Location of file to open
Location = Worksheets("Main").Range("folder_location").Value
'Open F&V File
Application.Workbooks.Open Location & Range("fv_file").Value
'Run Macro
Run ("Single_sector")
End Sub
Place the following code in the macro calling the other workbook:
Location = Worksheets("Main").Range("folder_location").Value
Set wb = Workbooks.Open(Location & Range("fv_file").Value)
Application.Run "'" & wb.Name & "'!" & strSubToRun, Parameters
Set wb = Nothing
Parameters is an array of arguments that you want to pass, so the sub in the other workbook should look something like
Public Sub TheSub(ParamArray X())
Dim i As Long
Sheet1.Cells(1, 1).Value = "Parameters passed:"
For i = 0 To UBound(X(0))
Sheet1.Cells(i + 2, 1).Value = CStr(X(i))
Next
End Sub
Probably not very elegant but:
Dim Location As String
Location = "\\location\to\file.xlsm"
Workbooks.Open(Location).RunAutoMacros (xlAutoOpen)
Where you have an Auto_Open Sub in your other excel file to handle the macros to run on your other spreadsheet
Please make sure your code in another workbook is at Workbook_open event so you dont need to use Run ("Single_sector"). The procedure single_selector would trigger as soon as another workbook is open.
Updated answer
Sub run_all()
Dim Location
On Error Resume Next
Dim wkb As Workbook
'Location of file to open
Location = Worksheets("Main").Range("folder_location").Value
'Open F&V File
Set wkb = Workbooks.Open(Location & Range("fv_file").Value)
wkb.Sheets(1).Single_sector ' kindly put this proc in another workbook sheet1
End Sub
I have a project that keeps growing and growing and growing. I'm at the last bit and it isn't looping. I'm not seeing why. I have the elements in their own macros and they perfectly until I try to combine them into a super macro that runs them all. The idea is that the "master" workbook (XLSM) will run its own update, THEN Open all other XLSM files in the same folder and run THEIR Updates which pushes data to other XLSX files. Right now each XLSM file runs a macro that works perfectly by
Sub EndofDay ()
Call step 1
Call Step 2
End Sub
I used the same "Do while" structure for the SuperMacro that opens the other books and calls their "EndofDay". it gets through a couple of the XLSM files then stops with an error on MyFiles = Dir. the following syntax SHOULD run the masters updates and one other code string, THEN open all other XLSM files in the Folder. Why does it suddenly not work at this level when It works on the level under this.
Sub SuperMacroEOD_Trans()
Dim MyFiles As String
Call EndofDayTransfer 'Do this Workbook Transfer first then:
'Step 2: Specify a target folder/directory.
MyFiles = Dir("C:\Users\ME\Desktop\QA VBA Project\*.xlsm")
'Dont try to open this workbook and do anything.
Do While MyFiles <> "" And MyFiles <> "C:\Users\ME\Desktop\QA VBA Project\Update_Master.xlsm"
'Step 3: Open Workbooks one by one
Workbooks.Open "C:\Users\ME\Desktop\QA VBA Project\" & MyFiles
Call EndofDayTransfer 'Call same macro you ran and run in the other workbooks (they contain it).
ActiveWorkbook.Close SaveChanges:=True
'Step 4: Next File in the folder/Directory
Loop
MyFiles = Dir <------------Gets stuck here or editor turns it yellow.
End Sub
There are a few logic mistakes in your code.
1- Here you're telling the code to stop when reached the master file, but what you actually want is to skip the master file. also you are comparing a simple filename to a fullpath name
Do While MyFiles <> "" And MyFiles <> "C:\Users\ME\Desktop\QA VBA Project\Update_Master.xlsm"
2- This should be put inside the loop, that is, before the line Loop
MyFiles = Dir <------------Gets stuck here or editor turns it yellow.
3- you are using ActiveWorkbook which is pretty hazardous. you should use explicit references. Moreover since all the workbooks have the procedure "EndofDayTransfer", including the master WB, it is mandatory to specify explicitly the "scope" so that the appropriate procedure is run (see Application.Run in the code below)
Sub SuperMacroEOD_Trans()
Dim MyFiles As String, wb As Workbook
Call EndofDayTransfer ' Master Workbook Transfer first
MyFiles = Dir("C:\Users\ME\Desktop\QA VBA Project\*.xlsm")
Do While MyFiles <> ""
If MyFiles <> ThisWorkbook.Name Then ' skip the master
' Always get an explicit reference to any file you open and use it
Set wb = Workbooks.Open("C:\Users\ME\Desktop\QA VBA Project\" & MyFiles)
Application.Run "'" & wb.name & "'!EndofDayTransfer" ' <-- specify scope explicitly to disambiguate
wb.Close SaveChanges:=True
End If
MyFiles = Dir ' <-- inside the loop
Loop
End Sub
I would greatly appreciate your help with a macro that I am trying to create.
I have a pathway that looks as follows: K:\XXX\XXX\XXX\Module 1
Module 1 is a folder that contains a bunch of xlsm files named with a number (i.e. 100.xlsm, 110.xlsm, and so forth)
I would like to create a loop that:
Runs the macro in workbook 100.xlsm;
Saves the 100.xlsm (NOT "save as") when the macro is done running;
Closes the saved xlsm, moves on to the next file (i.e.
110.xlsm), and repeats the same steps.
Before running the loop, I would like to create a statement that stores the names of those xlsm files.
The macro below may give you an idea of what I am after. There are indeed several errors.
Sub update()
Dim path As String path = "K:\XXX\XXX\XXX\Module 1"
Dim list() As Integer
List=(100, 110, 137, 140)
For Each n As Integer In list
Application.Run (path & "\" &n.xslm!refresh)
Save WORKBOOK
Close WORKBOOK
Next
End Sub
I think something like the code below will achieve what you are wanting to do.
Note that the code first opens the workbook whose macro you want to run.
You can then run the macro in that opened workbook from your original workbook with the Application.Run() command, e.g.
Application.Run("book1.xlsm!mymacro"), or
result = Application.Run("book1.xlsm!mymacro", "Hello", 20)
The second example calls a macro that requires a string paramater and an integer parameter.
The fuller example below opens some specific workbooks called 100.xlsm, 110.xlsm, etc and then runs a macro in each of them called SayHelloMessage.
I hope this helps.
Sub RunMacrosInOtherWorkbooks()
Dim wbpath As String 'path where the workbooks containing the macros you want to run are saved
Dim wbnames() As String 'array containing names of workbooks whose macros you want to run
Dim wbTarget As Workbook 'current workbook who macro is being run
Dim macroname As String 'name of macro being run
wbpath = "C:\Test"
wbnames() = Split("100.xlsm,110.xlsm,137.xlsm,140.xlsm", ",") 'Just one way of creating the list of workbooks.
macroname = "SayHelloMessage"
Dim i As Integer
Dim result As Variant
For i = 0 To UBound(wbnames)
Set wbTarget = Workbooks.Open(wbpath & "\" & wbnames(i))
result = Application.Run(wbTarget.Name & "!" & macroname)
' result = Application.Run(wbTarget.Name & "!" & macroname, 76) 'calling a subroutine or function with an argument. You need something to catch a return code
wbTarget.Save
wbTarget.Close
Next
End Sub
I've got probably what is a simple question but I can't figure out what's going wrong. I'm trying to create one big Macro in excel that will do multiple things to multiple files. The context:
The excel sheet I am running the Macro from is in its own folder. I've set up a little testing folder with this structure:
C:\Users\schris\Desktop\Eprime Testing\
This folder has two folders in it:
\Master Dataset\
In this folder is where the excel file with the macro is
\Eprime Processing\
There are three folders in this folder, named 'Fear', 'Gender', and 'Happy'. In each of those folders is an excel file that I wish to open.
What I wanted to do was create a Sub RunAll that would call various other subs because there are many different things I want the Macro to do, and I wanted to keep it organized.
So:
Sub RunAll()
Call OpenWorkbooks
Call ProcessFear
Call ProcessGender
Call ProcessHappy
End Sub
Here is my OpenWorkbooks code:
Sub OpenWorkbooks()
Dim wb1 As Workbook
Dim wbFear As Workbook
Dim wbGender As Workbook
Dim wbHappy As Workbook
Dim FileToOpen As Variant
Dim FileToOpen2 As Variant
Dim FileToOpen3 As Variant
Dim Sheet As Worksheet
' Must be workbook with the Macros I'm running
Set wb1 = ActiveWorkbook
' Opens Fear
ChDir "C:\Users\schris\Desktop\Eprime Testing\Eprime Processing\Fear"
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose Fear file")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wbFear = Workbooks.Open(fileName:=FileToOpen)
End If
Set wbFear = ActiveWorkbook
' Opens Gender
ChDir "C:\Users\schris\Desktop\Eprime Testing\Eprime Processing\Gender"
FileToOpen2 = Application.GetOpenFilename _
(Title:="Please choose Gender file")
If FileToOpen2 = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wbGender = Workbooks.Open(fileName:=FileToOpen2)
End If
Set wbGender = ActiveWorkbook
' Opens Happy
ChDir "C:\Users\schris\Desktop\Eprime Testing\Eprime Processing\Happy"
FileToOpen3 = Application.GetOpenFilename _
(Title:="Please choose Happy file")
If FileToOpen3 = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wbHappy = Workbooks.Open(fileName:=FileToOpen3)
End If
Set wbHappy = ActiveWorkbook
End Sub
Now I want to be able to reference these three workbooks (wbFear, wbGender, wbHappy) and move seamlessly between them. When testing in Sub OpenWorkbooks(), doing wbFear.Activate would work and process correctly... But when I separated out the tasks of each macro (i.e., OpenWorkbooks now only opens the workbooks, ProcessFear only processes the data in the Fear workbook), I get a Run-time error '91': Object variable or With block variable not set.
I'm assuming this has something to do with the declared workbook names being 'lost' as it switches Subs, but when I put the code from OpenWorkbooks into RunAll and only had ProcessFear run, it still couldn't activate the proper workbooks.
Basically, my question is this:
How can I have one Macro open three workbooks, and declare them all as something that other macros can reference? There are many tasks I need to do, so I really want to have separate Subs for each one, that way I don't get lost in the code.
Thank you for your time!
There are better, more elegant solutions. But this is the simplest and also the easiest for you to implement. Declare 3 global object variables to hold the workbook references. Open the workbooks and assign to these variables in OpenWorkbooks. Use them as needed. Close them and set them to Nothing in a new procedure CloseWorkbooks. (Call is not needed in this context)
Public gwbFear as WorkBook
Public gwbGender as WorkBook
Public gwbHappy as WorkBook
Sub RunAll()
OpenWorkbooks
ProcessFear
ProcessGender
ProcessHappy
CloseWorkbooks
End Sub
An employee at one of my company's local offices is working on a macro in Sheet1 of a workbook that would run a macro in another workbook using Application.Run:
Private Sub CommandButton1_Click()
Wfile = Range("B2").Value
Wpath = Range("B3").Value
Workbooks.Open Wpath + "/" + Wfile
Application.Run Wfile & "!copy_rates_macro"
End Sub
The workbook that he is trying to open/use is protected in every way possible (all of its sheets are protected and its VBAProject is protected as well.
When the macro is run, the 1004 run-time error message window pops up saying "Cannot rund the macro 'Name.xlsm!copy_rates_macro'. The macro may not be available in this workbook or all macros may be disabled.'
I did a lot of research and I thought the putting the following in the protected file would work:
Private Sub Workbook_Open()
' Dim wSheet As Worksheet
' For Each wSheet In Worksheets
' wSheet.Protect Password:="pw", UserInterFaceOnly:=True
' Next wSheet
Application.EnableEvents = False
End Sub
Note that the parts commented out above are my additions to code that was already there and must remain there. Also, "pw" is the password for every worksheet and the VBAProject.
This code didn't make a difference (it wasn't commented out when I ran it), and I imagine it has something to do with the VBAProject being protected.
Is this request even possible, or is it a lost cause? My boss doesn't want the password to the protected workbook to be released but I can't see a way around it.
Thanks for any help.
You shouldn't have to unprotect a project to run code in it (that would make it pretty worthless), but I can see a couple of possible issues in your code. First you use "/" at the end of the path rather than "\" and second, if the workbook name contains spaces you need to enclose it in single quotes:
Workbooks.Open Wpath + "\" + Wfile
Application.Run "'" & Wfile & "'!copy_rates_macro"
If that still won't run, there are a few possible pitfalls:
1. The macro is in a module of the same name, or in an object module, such as a worksheet or ThisWorkbook, in which case you need to prefix the macro name with the code name of the object.
2. Automation Security may be disabling macros in the opened workbook. To work around that, try adding:
Application.AutomationSecurity = msoAutomationSecurityLow
before opening the workbook. Ideally, you should store the current value and reset that at the end.