简体   繁体   English

使用 MS Access 执行 Excel VBA

[英]Use MS Access to execute Excel VBA

I need to call and execute VBA in an Excel spreadsheet from an Access form+button using VBA.我需要使用 VBA 从 Access 表单+按钮在 Excel 电子表格中调用和执行 VBA。

I have several Excel spreadsheets that are connected to my Access DB.我有几个连接到我的 Access DB 的 Excel 电子表格。 I created a separate DB with VBA (C:\\CDR & Project Inventory Reports\\RebuildDB) to refresh and rebuild my source databases and report workbooks.我用 VBA (C:\\CDR & Project Inventory Reports\\RebuildDB) 创建了一个单独的数据库来刷新和重建我的源数据库和报告工作簿。 I also created a separate workbook to refresh all of my other reports (C:\\CDR & Project Inventory Reports\\RefreshReports).我还创建了一个单独的工作簿来刷新我的所有其他报告(C:\\CDR & Project Inventory Reports\\RefreshReports)。 Module 15 of that .xlsm will call all of my other modules to refresh those reports.该 .xlsm 的模块 15 将调用我的所有其他模块来刷新这些报告。

Prior to executing the Excel portion of the code is all of my code pertaining to rebuilding the DB.在执行代码的 Excel 部分之前,我的所有代码都与重建数据库有关。 Currently, the Excel portion looks like this:目前,Excel 部分如下所示:

Dim oXLApp As Object
Set oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
oXLApp.Workbooks.Open ("C:\CDR & Project Inventory Reports\RefreshReports.xlsm")
Debug.Print "Open report refresh code"
Debug.Print "refreshing all spreadsheets"

oXLApp.Run "RefreshReports.Module15" 'refreshes all CDR and Proj Inv spreadsheets

DoEvents
Debug.Print "Refresh completed"
oXLApp.ActiveWorkbook.Close (True)
oXLApp.Quit
Set oXLApp = Nothing
Debug.Print "Release object"

The problem I'm having is that oXLApp.Run doesn't actually execute the VBA it needs to.我遇到的问题是 oXLApp.Run 实际上并没有执行它需要的 VBA。 It calls it, then immediately closes.它调用它,然后立即关闭。 Is there any other way to call it that would result in the code actually running?有没有其他方法可以调用它来导致代码实际运行?

oXLApp.Run "RefreshReports.Module15"

should be应该

oXLApp.Run "RefreshReports.xlsm!NameOfMySub" '// Change sub name as required

As it stands, your code isn't running any VBA from Excel because you haven't referred to a specific sub.就目前而言,您的代码没有从 Excel 运行任何 VBA,因为您没有引用特定的子程序。 I presume you have error handling switched off as this should be rather noticeable.我认为您关闭了错误处理,因为这应该很明显。

You should fully qualify the sub to be executed, by also giving the module name so您应该通过提供模块名称来完全限定要执行的子程序

oXLApp.Run "RefreshReports.Module15"

should be应该

oXLApp.Run "'RefreshReports.xlsm'!Module15.NameOfMySub"

If you use如果你使用

oXLApp.Run "'RefreshReports.xlsm'!NameOfMySub"

and the procedure NameOfMySub is declared in more than one module you will get and error message.并且过程NameOfMySub是在多个模块中声明的,您将收到错误消息。

You could also re-write your code thus:你也可以这样重写你的代码:

Dim oXLApp As Object
Dim Wkbk as workbook

Set oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
set Wkbk  = oXLApp.Workbooks.Open ("C:\CDR & Project Inventory Reports\RefreshReports.xlsm")

Debug.Print "Open report refresh code"
Debug.Print "refreshing all spreadsheets"

oXLApp.Run "'" & Wkbk.Name & "'!Module15.NameOfMySub"

' Note use of single speechmarks arond workbook name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM