阿尼爾稱它,你就宣告X選擇工作簿複製粘貼作爲一個工作表
Dim x As Worksheet
但你想將其設置爲一個字符串
A = "Portfolio"
For i = 1
Set x = A & i
您還做了爲W同樣的事情,除非是因爲工作簿
也許你可以試試
set w = Workbooks.Open(<path>\<filename>)
set x = w.sheets(A & I)
如果在細胞(2,1)值的非數字,你會得到一個類型不匹配錯誤。
這片頂部會給你一些問題
enter code here
這可能是更適合您在您的評論中提到的:在使用 Debug.Print:
Sub test()
Dim workBookPath As String, filename As String
Dim i As Long, j As Long
Dim awb As Workbook, w As Workbook
Dim x As Worksheet
Set awb = ActiveWorkbook
workBookPath = "C:\users\mt390d\Documents\Reports\"
If IsNumeric(Cells(2, 1)) Then
j = Cells(2, 1).Value
Else: MsgBox ("Cell A2 must contain a number")
Exit Sub
End If
For i = 1 To j
filename = Dir(workBookPath)
If filename <> awb.Name Then
Set w = Workbooks.Open(workBookPath & filename)
Sheets("Download1").Copy awb.Sheets(1)
Set x = ActiveSheet
On Error Resume Next
x.Name = "Portfolio" & i
On Error GoTo 0
w.Close
End If
filename = Dir()
Next i
End Sub
設置x = A&i,會拋出編譯錯誤:類型不匹配,這可以自己嘗試。 – Anil
此外,您還沒有在任何地方聲明'B' – newguy