2017-09-29 17 views
0

以下は、複数のファイルを開いて、いくつかのアクションを実行するためのコードです。私は助けが必要な部分だけを言及しています。複数選択vba

Sub Sample() 
Dim myFile As Variant 
Dim i As Integer 

myFile = Application.GetOpenFilename(MultiSelect:=True) 

If IsArray(myFile) Then 
    For i = LBound(myFile) To UBound(myFile) 
    Set mywkbook= Workbooks.Open(myFile(i)) 
    Next i 
End If 
End Sub 

これはうまくいきます。 しかし、私は "mywkbook"変数が別のワークブックの値を持っていて、私はそれらを扱うことができます。 助けてください

答えて

0

は、私はそれがこのようにやや働かせた:それは将来的に誰かを助けることができるように:)

を私の答えを投稿

Dim paths(), wbs() As Workbook 
Dim x As Integer 

paths = Application.GetOpenFilename(FileFilter:="Excel Files 
(*.XLSX),*.XLSX", MultiSelect:=True) 


    For x = 1 To UBound(paths) 
    ReDim wbs(UBound(paths)) 
Set wbs(x) = Workbooks.Open(paths(x)) 

    With wbs(x).Sheets("Sheet1") 

    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row 

.Range("A2:X" & lastrow).Copy newexcel.Sheets("RawData").Range("A" & 
newexcel.Sheets("RawData").Rows.Count).End(xlUp).Offset(1) 

End With 
Next x 

0

スカラー変数は同時に異なる値を持つことはできませんので、配列を使用する必要があります。あなたはそのようなあなたのコードを変更することによってこれを行うことができます:

Sub Sample() 

    Dim myFile As Variant 
    Dim i As Integer 
    Dim mywkbooks() as Workbook 

    myFile = Application.GetOpenFilename(MultiSelect:=True) 

    If IsArray(myFile) Then 

    For i = LBound(myFile) To UBound(myFile) 
    Set mywkbooks(i) = Workbooks.Open(myFile(i)) 
    Next i 

    End If 

End Sub 
0

うまくいけば、これはこれは、このフォーラムで私の最初の回答で、[OK]を伝わってきます。

Sub tryThis() 

Dim myFile As Variant 
Dim i As Integer 
Dim workbookNames() As String ' define a empty string array variable to hold the file names 

myFile = Application.GetOpenFilename(MultiSelect:=True) 

If IsArray(myFile) Then 
    For i = LBound(myFile) To UBound(myFile) 
     Workbooks.Open (myFile(i)) ' Just open the workbook here 
     ReDim Preserve workbookNames(i) ' ReDim redefines your array to 'i' number of elements. Preserve ensures you don't lose the previous values held in the Array 
     workbookNames(i) = ActiveWorkbook.Name ' Assign the Name of the Workbook to the Array 
      MsgBox workbookNames(i) ' you don't need this, I just used it to demonstrate that you now have the file name and can work with it 
    Next i 
End If 

End Sub 

編集:私はこれを「Binarus」と同時に結んでいました。ワークブック配列を宣言するアプローチが好きですが、 'Set mywkbooks(i)'の前に次の行を追加する必要があります。そうしないと、配列が空で、まだ値を保持できないため、添字が範囲外です。

ReDimステートメント保存mywkbooks(I)