PDA

View Full Version : Run macro from an already open workbook



rockybalboa
08-28-2013, 12:07 AM
Hi,

I need a code to run macro from an already open workbook without giving file name.

I know there is a code "Application.Run ("'" & workbookname & "'!macroname")" but i donot want to give workbookname as the file name will keep on changing.

Thanks

Aflatoon
08-28-2013, 02:21 AM
Try:
Application.Run "macroname"

rockybalboa
08-28-2013, 03:10 AM
What i am trying to do is open all the excel files in a folder and run the macro in each file one by one.

i am stuck here. Need a code to run a macro without mentioning the file name.

Sub AllFiles() Dim folderPath As String
Dim filename As String
Dim wb As Workbook

folderPath = ThisWorkbook.Sheets("SHeet1").Range("D2").Value

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

filename = Dir(folderPath & "*.xlsm")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)


Application.Run "Committee_Tag"
filename = Dir

Loop
Application.ScreenUpdating = True
End Sub

Aflatoon
08-28-2013, 03:39 AM
But you do know the file name so you can simply use it:
Application.Run "'" & filename & "'!Committee_Tag"

rockybalboa
08-28-2013, 04:34 AM
but the file names will keep on changing as i will be opening all the excel files in a folder to run the macro.

Aflatoon
08-28-2013, 04:53 AM
I know - but your filename variable is the name of the file you opened.

rockybalboa
08-28-2013, 06:14 AM
Thank you