Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

run macro from another file

0

hello

I  try  finding way  to  run  the  macro  from  another  file   whether  closed or  opened. 

my  requierments are   if  the  file  is  closed  which   contain  the  macro  then  open  and  run  the  macro  to  another  file  and   closed after  run  the  macro .

this  is  what  I  have   but  it  gives  error   1004 file  could  not  be  found .

I'm  sure  about  the  file  name  and  the  macro    name  . two  files  are  found  in the  same  folder  .

Ask Question  
Private Sub myMacro()
 Dim wb As Workbook
 On Error Resume Next
 Set wb = Workbooks("boo1.xlsm")
 On Error GoTo 0
 If wb Is Nothing Then Set wb = Workbooks.Open(ThisWorkbook.Path & "\move.xlsm")
 Run "move.xlsm!test"
 wb.Close False
 Set wb = Nothing
End Sub

thanks

Answer
Discuss

Answers

0
Selected Answer

Your code calls for

Run "move.xlsm!test"

But this isn't what you prepared for. Your prepared for the procedure to be in the workbook Wb. Therefore the function call should be.

Run Wb.Name & "!test"

This much for the logic. I haven't tested the idea and therefore don't know if it works. Have you considered placing the code in a code library? Check this out.

EDIT:-

Some other suggestions from the web recommend that you as single inverted commas to the workbook's name.

Run "'" & Wb.Name & "'!test"
Discuss

Discussion

thanks.  unfortunately it doesn't work and I know about the personal macro but I have curiosity if there is way do that by vba
speed (rep: 40) Jul 4, '21 at 7:03 am
variatus
my  apology !  this  is  my  mistake .     the  macro  name  is  wrong  that's  why  shows  the  error .  so   you  first  answer  works  excellantly 
thanks   for   your  answering
speed (rep: 40) Jul 4, '21 at 4:08 pm
Hello Speed,
Thanks for the feedback. I also learn :-)
Variatus (rep: 4889) Jul 5, '21 at 2:54 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login