简体   繁体   English

如何使用VBA在Excel中添加新工作表?

[英]How to add a new sheet in excel using VBA?

I am new to VBA coding. 我是VBA编码的新手。 I want to populate an excel sheet with different data. 我想用不同的数据填充excel表。 The requirement is to pull data into separate sheets of the same Excel. 要求是将数据拉入同一Excel的单独表格中。 I need to use the same type of code below to maintain the integrity of the entire code. 我需要使用下面相同类型的代码来维护整个代码的完整性。

outfile = "c:\" & Format(Date, "yyyymmdd") & "activedirectorydata.xls"
DoCmd.OutputTo acOutputQuery, "**MoActiveDirectoryData**", acFormatXLS, outfile, False

I am able to get data from a view/query MoActiveDirectoryData into one sheet. 我能够将视图/查询MoActiveDirectoryData导入一个工作表。 How should I go about getting data from another view/query into a different sheet of the same excel? 我应该如何将来自另一个视图/查询的数据转换为同一个Excel的不同表单?

If you use DoCmd.TransferSpreadsheet, sheets will be created in the same file as long as the table or query that you are exporting has a different name than the sheets that already exist. 如果使用DoCmd.TransferSpreadsheet,只要要导出的表或查询的名称与已存在的表不同,就会在同一文件中创建工作表。

outfile = "c:\" & Format(Date, "yyyymmdd") & "activedirectorydata.xls"
''Sheet is called Query1
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Query1", outfile 
''New sheet called Query2
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Query2", outfile 

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

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