以下代码应该将名为PCReport的工作簿中的所有工作表(除了最后2个)复制到名为Insp&的新工作簿中。日期。
它落在了行上
Workbooks(xWkb).Sheets(sheet.Name).Copy after:=Workbooks(tgtWkb).Sheets(x)
错误是运行时错误424,需要对象。
x = 1,总数= 10,xWkb =“PCReport.xlsm”,tgtWkb =“Insp25112015.xls”
Dim total As Integer
Dim NewWkb As Workbook
Dim xWs As Worksheet
Dim xWkb As String
Dim tgtWkb As String
Dim i As Integer
xWkb = "PCReport.xlsm"
Set NewWkb = Workbooks.Add
'ActiveWorkbook.SaveAs "C:\Users\Carol\Desktop\Insp" & Format(Date, "ddmmyyyy") & ".xls"
ActiveWorkbook.SaveAs "C:\Users\Carol.Hedges\Insp" & Format(Date, "ddmmyyyy") & ".xls"
tgtWkb = ActiveWorkbook.Name
total = Workbooks(xWkb).Worksheets.Count
i = 1
For x = 1 To total - 2
Workbooks(xWkb).Sheets(sheet.Name).Copy after:=Workbooks(tgtWkb).Sheets(x)
i = i + 1
Next
答案 0 :(得分:1)
这可能会对你有所帮助,你不需要像Paresh所建议的那样,在使用while循环时它可能很有用但不在这里:
Dim NewWkb As Workbook
Dim xWkb As Workbook
Dim x as Integer
Set xWkb = Workbooks("PCReport.xlsm")
Set NewWkb = Workbooks.Add
NewWkb.SaveAs "C:\Users\Carol.Hedges\Insp" & Format(Date, "ddmmyyyy") & ".xls"
For x = 1 To xWkb.Worksheets.Count - 2
xWkb.Sheets(x).Copy after:=NewWkb.worksheets(NewWkb.worksheets.count)
Next x