0

複数のワークブックから、すべて同じシート インデックスからデータをインポートしたい (3)。私はvbaが初めてで、複数のファイルを開く方法と、1つのファイルの別のワークブック内の1つのシートから別のシートにデータをコピーする方法を理解しましたが、その方法を理解できないようです複数のファイルの場合。エラーの場所を強調表示すると、「オブジェクトはこのプロパティまたはメソッドをサポートしていません」と表示されます

助けていただけますか?ありがとう

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
4

3 に答える 3

0

ラインを変えてみるSet ArbinBook = Workbooks.Open(ArbinFile(i))

Set ArbinBook = Workbooks(arbinfile(i))

私は間違っている可能性がありますが、ワークブック オブジェクトをワークブックとしてラベル付けするのではなく、別のワークブックを開くアクションになるように設定しようとしていると思います。

于 2013-06-20T21:02:14.990 に答える
-1
Sub Multiple()
Application.DisplayAlerts = False
Application.EnableEvents = False
Dim exlApp As Excel.Application
Dim exlWb1 As Excel.Workbook
Dim exlWb2 As Excel.Workbook
Dim exlWb3 As Excel.Workbook
Dim exlWs1 As Excel.Worksheet
Dim exlWs2 As Excel.Worksheet
Dim exlWs3 As Excel.Worksheet
Set exlApp = CreateObject("Excel.Application")
Set exlWb1 = exlApp.Workbooks.Open("C:\yourpath1\file1.xls")
Set exlWb2 = exlApp.Workbooks.Open("C:\yourpath2\file2.xls")
Set exlWb3 = exlApp.Workbooks.Open("C:\yourpath3\file3.xls")
Set exlWs1 = exlWb.Sheets("Sheet1")
Set exlWs2 = exlWb.Sheets("Sheet1")
Set exlWs3 = exlWb.Sheets("Sheet1")
exlWb1.Activate
exlWb2.Activate
exlWb3.Activate
'code
exlWb.Close savechanges:=True
exlWb.Close savechanges:=True
exlWb.Close savechanges:=True
Set exlWs1 = Nothing
Set exlWs2 = Nothing
Set exlWs3 = Nothing
Set exlWb1 = Nothing
Set exlWb2 = Nothing
Set exlWb3 = Nothing
exlApp.Quit
Set exlApp = Nothing
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
于 2013-06-20T23:57:36.757 に答える