Call a function from another workbook

superchao

New Member
Joined
Nov 12, 2007
Messages
2
the purpose is to call a function (name is repeat) from another workbook wth path('D:\EXcel VBA\practice\trial.xls'). I find the following is successful.


Private Sub CommandButton1_Click()

Workbooks.Open("D:\EXcel VBA\practice\trial.xls")
Application.Run "'D:\EXcel VBA\practice\trial.xls'!repeat"

End Sub

However, When I try to use a string "file" to replace the long name 'D:\EXcel VBA\practice\trial.xls' It fails as shown below. error is with the last statement Application.Run 'file!repeat'
Help needed. thanks!!!


Private Sub CommandButton1_Click()

Dim wb As Workbook
Dim file, As String

file = "D:\EXcel VBA\practice\trial.xls"
Workbooks.Open file
Application.Run 'file!repeat'

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Well that's probably because you've not actually used the variable file you've used the word file.
Code:
Application.Run file & "!repeat"
 
Upvote 0
First do a check if the file is open...

Code:
Function WbOpen(wbName As String) As Boolean
'Originally found by Jake Marx
    On Error Resume Next
    WbOpen = Len(Workbooks(wbName).Name)
End Function

Use like this ...

Code:
dim wb as workbook, blnOpened as boolean
if wbopen("trial.xls") = true then set wb = workbooks("trial.xls")
if wb is nothing then
    set wb = workbooks.open(file)
    blnopened = true
end if

'.. other code, i.e.
application.run file & "!ProcName", "Arg1"

if blnopened = true then wb.close

HTH
 
Upvote 0
I Tried Application.Run file & "!repeat"
The error msg is as following
The macro 'D:\EXcel VBA\practice\trial.xls!repeat' cannot be found

"repeat" is a function stored in modules in "trial" And I tried again"repeat" can be called in the following method. Application.Run "'D:\EXcel VBA\practice\trial.xls'!repeat”

Any idea??? help!!!!
 
Upvote 0
Try adding the module name...

Code:
...file & "!trial.repeat"

Use an exclamation after the filename, use a period to separate the module and routine name.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,532
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top