Controlling Excel workbook from Access 2010 VBA

ぐ巨炮叔叔 提交于 2019-12-08 03:31:34

问题


I have a situation very similar to the following post:

Access query to excel 2010 to create a graph via vba

In my case, I export a table, but I want to do a lot more to the Excel file.

First I want to rename the tab on the first sheet of the Excel file, since it exports with the funky table name. So, instead of "tblThisIsMyTable", I'd like to change it to "MyTable as of (add a date variable here)"

Secondly, I need to create a Pivot table and make a copy of the Pivot table on another tab, to change one of the parameters.

My question is, can I record a macro in Excel to do the basic steps and drop that code into my Access VBA?

I hope I was clear enough and thanks for any help you can provide.


回答1:


My question is, can I record a macro in Excel to do the basic steps and drop that code into my Access VBA?

Yes, absolutely.

From Access, you may need to add a reference to the Microsoft Excel library.

You will then need to make some changes, to the "recorded" macro so that it creates a new instance of Excel.Application, and opens the desired file & worksheet. E.g.,

Sub OpenExcel()
Dim xlApp as Excel.Application
Dim xlWB as Excel.Workbook
Dim xlSh as Excel.Worksheet

Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Your filename.xlsx") '<modify as needed
Set xlSH = xlWB.Sheets("Sheet Name") '<modify as needed

'You can paste the recorded macro in here
'
'
'
'
'

xlWB.Save
xlWB.Close
xlApp.Quit
Set xlApp = Nothing

End Sub


来源:https://stackoverflow.com/questions/15958061/controlling-excel-workbook-from-access-2010-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!