在vba中复制并重命名Excel ActiveSheet

时间:2017-04-10 12:17:42

标签: excel vba ms-access export

我在访问中生成一个命令按钮,将数据导出到Excel中。目前,我有这个功能工作,并试图完善它,以便每个导出将数据导出到一个新的工作表,这将有效地是ActiveSheet的副本。似乎让我失望的代码是:

'Open workbook

Excel.Workbooks.Open "\\aos03s-fp04\brewsti2$\Alarms\ExportTest - " & Format(Now(), "dd-mm-yyyy") & ".xlsx", True, False

'Copy initial activesheet

ActiveSheet.Copy Before:=Excel.Sheets("Sheet2")

'Rename new copied sheet

ActiveSheet.Name = Me.RAW_TAG

ActiveSheet.Copy函数的代码失败。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

Access具有构建命令按钮过程以将表导出到Excel。但是,无论如何,你可以使用这个:

Dim outputFileName As String
outputFileName = CurrentProject.Path & ".xls"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", outputFileName , True

尝试这个,或者:

Sub CopySheet()
Dim MySheetName As String 
MySheetName = "TestSheet" Sheets("MasterSheet").Copy After:=Sheets("MasterSheet") 
ActiveSheet.Name = MySheetName
End Sub

答案 1 :(得分:0)

尝试这个,或者:

Sub CopySheet()
Dim MySheetName As String 
MySheetName = "TestSheet" Sheets("MasterSheet").Copy After:=Sheets("MasterSheet") 
ActiveSheet.Name = MySheetName
End Sub

答案 2 :(得分:0)

假设此代码在MS Access VBA中运行,并且Excel是Excel Application对象,则需要指定ActiveSheet是该Application个对象的属性:

'Open workbook

Excel.Workbooks.Open "\\aos03s-fp04\brewsti2$\Alarms\ExportTest - " & Format(Now(), "dd-mm-yyyy") & ".xlsx", True, False

'Copy initial activesheet

Excel.ActiveSheet.Copy Before:=Excel.Sheets("Sheet2")

'Rename new copied sheet

Excel.ActiveSheet.Name = Me.RAW_TAG