VBA控制Excel工作簿

VBA控制Excel工作簿

本文介绍了从Access 2010 VBA控制Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况非常类似于下面的帖子:

Access查询到Excel 2010中创建通过VBA

在我的情况,我导出表,但我想要做更多的Excel文件。

首先,我要重命名的选项卡中的Excel文件的第一页上,因为它导出与时髦的表名。而不是tblThisIsMyTable所以,我想将它更改为MyTable的作为(这里补充一个日期变量)

其次,我需要创建一个数据透视表,并在另一个选项卡中的数据透视表的副本,更改的参数之一。

我的问题是,我可以记录在Excel宏做的基本步骤,并丢弃code到我的访问VBA?

我希望我很清楚,感谢您的帮助,您可以提供。

解决方案

是的,绝对的。

从Access,您可能需要添加一个引用到Microsoft Excel中的库。

您一定要去作一些修改,以记录宏以便它创建Excel.Application的一个新实例,并打开所需的文件和放大器;工作表。例如,

 子OpenExcel()
昏暗xlApp作为Excel.Application
昏暗xlWB为Excel.Workbook
昏暗xlSh为Excel.Worksheet

设置xlApp =新Excel.Application
设置xlWB = xlApp.Workbooks.Open(C:\您的filename.xlsx),'<关于需要进行修改
设置xlSH = xlWB.Sheets(表名),'<关于需要进行修改

你可以粘贴录制的宏在这里






xlWB.Save
xlWB.Close
xlApp.Quit
设置xlApp =什么

结束小组
 

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.

解决方案

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

这篇关于从Access 2010 VBA控制Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 15:39