以下代碼應該將工作簿中名爲PCReport的所有工作表(除了最後2個)複製到名爲Insp & date的新工作簿。將多個工作表複製到不同的工作簿
它翻倒就行
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
其中'x'變量來自我認爲你使用變量'我'? – 2014-11-25 05:52:00
'sheet.Name'的sheet應該是什麼?你不要聲明它。 – 2014-11-25 06:02:26
將行更改爲'Workbooks(xWkb).Sheets(x).Copy after:= Workbooks(tgtWkb).Sheets(x)'。注意:使用索引可能會很棘手,也許會更好地循環所有工作表並添加一個條件以避免按名稱排列的最後兩個工作表。注2:我用什麼變量?,使用Option Explicit,一些變量沒有聲明。 – 2014-11-26 21:02:05