当我运行宏时,我得到运行时错误,并在调试时突出显示以下代码:
ThisWorkbook.Sheets("Template Summary Screen").Copy After:=wb.Sheets(2)
您认为怎么回事?
Sub saveasxlsx()
Dim wb As Workbook
Dim staticFolder As String
Dim dateformat As String
Dim wbfirst As Workbook
Set wbfirst = ThisWorkbook
wbfirst.RefreshAll
'location of the parent folder
staticFolder = "\\C:\Location\"
'provides previous month YYYYMM
dateformat = Format(DateAdd("M", -1, Date), "yyyymm", 1)
Set wb = Workbooks.Add
ThisWorkbook.Sheets("Template Summary Screen").Copy After:=wb.Sheets(2)
ThisWorkbook.Sheets("IPV").Copy After:=wb.Sheets(3)
ThisWorkbook.Sheets("ADJ").Copy After:=wb.Sheets(4)
ThisWorkbook.Sheets("Lists").Copy After:=wb.Sheets(5)
'save the new workbook in the folder
ActiveWorkbook.SaveAs staticFolder & "\" & dateformat & "\" & "Working files" & "\" & "GM AFS PE - UPLOAD VERSION" & " - " & dateformat & ".xlsx"
'close the workbook
ActiveWorkbook.Close
End Sub
答案 0 :(得分:1)
Set wb = Workbooks.Add
这会创建一个包含一个表的新工作簿,因此wb.Sheets(1)
是唯一有效的工作表,任何其他整数都超出范围。
要添加工作表,您需要使用wb.Sheets.Add()
答案 1 :(得分:0)
搞定了!出于某种原因,我的excel已经默认为单张新工作簿 - 只需将其更改为3并且工作正常