我想從多個工作簿中導入數據,這些工作簿全部來自同一張工作表索引(3)。 我是vba的新手,我想出瞭如何打開多個文件,並將數據從一張紙複製到另一張工作簿中的單個文件,但我似乎無法弄清楚如何爲多個文件做這件事。 我突出顯示錯誤的位置,它告訴我「對象不支持此屬性或方法」如何在for循環中爲excel定義一個對象vba
請問您可以幫忙嗎? 感謝
Sub dataimport()
' Set Vars
Dim ArbinBook As Workbook, DataBook As Workbook
Dim i As Integer, j As Integer
Dim Caption As String
Dim ArbinFile As Variant, DataFile As Variant
' make weak assumption that active workbook is the target
Set DataBook = Application.ActiveWorkbook
' get Arbin workbook
Caption = "Please select an input file"
' To set open destination:
' ChDrive ("E")
' ChDir ("E:\Chapters\chap14")
' With Application
'Set "arbinfile" as variant, the "true" at end makes it into an array
ArbinFile = Application.GetOpenFilename(, , Caption, , True)
'Exit when canceled
If Not IsArray(ArbinFile) Then
MsgBox "No file was selected."
Exit Sub
End If
Dim targetSheet As Worksheet
Set targetSheet = DataBook.Sheets(1)
'Open for every integer i selected in the array "arbinfile"
For i = LBound(ArbinFile) To UBound(ArbinFile)
Set ArbinBook = Workbooks.Open(ArbinFile(i))
targetSheet.Range("A2", "G150").Value = ArbinBook.Sheets(3).Range("A2", "G150").Value
**ERROR at the line above**
Workbooks(DataSheet).Activate 'Reactivate the data book
Worksheets(1).Activate 'Reactivate the data sheet
ActiveWorkbook.Sheets(1).Copy _
after:=ActiveWorkbook.Sheets(1)
Workbooks(ArbinFile(1)).Activate 'Reactivate the arbin book(i)
ArbinBook.Close
Next i
Beep
End Sub
嘗試不使用在任的範圍內來電 – Brad
'.value'是誤差在循環的開始發生,或當你要複製的範圍內? (上面或下面哪裏出現** ERROR HERE **的行?) –
嗨,對不起,錯誤出現在行「targetsheet.range ...」 –