Open Excel Macro from Access

erodjob

Active Member
Joined
Feb 11, 2003
Messages
253
If possible can someone please share code that will open a macro in Excel 97, from within a command button in Access 97?

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
This seems to work OK. It will run a procedure called 'TestProc' found in the file referred to below. You will need to set a reference to the Excel object library from within Access (click on Tools-References and find the Microsoft Excel x.0 Object Library).

Code:
Public Function OpenMacroFromExcel()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Set xlApp = New Excel.Application

With xlApp
    .Visible = True
    Set xlWB = .Workbooks.Open("C:\My Documents\Test.xls")
End With
 
xlWB.Application.Run "TestProc"

Set xlWB = Nothing
Set xlApp = Nothing

End Function
 
Upvote 0
Does it make a difference that I am using my personal macro workgroup?

I made a command button in access and placed the code there. I renamed the macro to FedCalc2.

However, I don't have a text.xls in My Documents, should I make one?

Public Function Command7_Click()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Set xlApp = New Excel.Application

With xlApp
.Visible = True
Set xlWB = .Workbooks.Open("C:\My Documents\Test.xls")
End With

xlWB.Application.Run "FedCalc2"

Set xlWB = Nothing
Set xlApp = Nothing

End Function
 
Upvote 0
Replace the C:\My Documents\Test.xls bit with the path and name of your orkbook, I just created a workbook called Test in that folder to make sure the code worked. Also, why is your command button's click event a Function? Surely it should be a Sub?
 
Upvote 0
I changed it to:

Public Sub Command7_Click()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Set xlApp = New Excel.Application

With xlApp
.Visible = True
Set xlWB = .Workbooks.Open("C:\My Documents\Test.xls")
End With

xlWB.Application.Run "FedCalc2"

Set xlWB = Nothing
Set xlApp = Nothing

End Sub

But i get Complile error
User-defined type not defined.

Why do i need to reference the workbook? The macro is in all workbooks not just one.
 
Upvote 0
Access doesn't know anything about Excel objects unless you tell it where to find the info, so you need to reference the Excel object model. See the second sentence in the first post.
 
Upvote 0

Forum statistics

Threads
1,221,586
Messages
6,160,645
Members
451,661
Latest member
hamdan17

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