打开多个

时间:2018-04-04 10:01:56

标签: excel vba

我有一个小问题,我希望能够将工作簿设置为像Set MainWorkBook = ActiveWorkBook这样的变量,这很简单,但是我现在必须能够打开多个独特的工作簿,范围从2到50左右取决于我使用代码的原因。

理想情况下,我希望将第一个文件选为1Book,然后将第二个文件选为2Book等。因此,稍后可以在代码中轻松引用它们

使用这个想法我需要使用for循环,比如

For i = 1 To NUMBEROFOPENEDWORKBOOKS

    WorkBookName = i & "Book"


    Set WorkBookName = ActiveWorkbook

Next i

其中,NUMBERIFOPENEDWORKBOOKS是变量的sudo代码,用于定义我已选择的工作簿数量,然后放在下面代码的For Each部分。

我使用下面的代码打开工作簿。

Set FileChoice = Application.FileDialog(msoFileDialogFilePicker) ' Opens a File Exploer dialog box '

With FileChoice ' This is used to set what is being displayed '

    .ButtonName = "Select"
    .AllowMultiSelect = True  ' Enables the abilty to select more than 1 file
    .InitialView = msoFileDialogViewDetails ' Sets the start location '
    .Show ' Allows it to be seen '

    For Each oFD In .SelectedItems ' This is used to create a file path used to open the file

        FilePath = oFD
        Workbooks.Open (FilePath)

    Next oFD

End With

我尝试使用.selectedItems在代码中添加for循环但是只确认它会失败

我希望所有这一切都有道理。 感谢您提供任何帮助。

编辑1
我已经采用了注释和trie的建议形式来使用Arraty函数来解决这个问题,但是当我运行下面的代码时,我收到Object Required错误。

我是否做了一个无法清除的错误,我无法看到?

Public WorkBookArray() As Excel.Workbook
Sub Channel_1()

Set FileChoice = Application.FileDialog(msoFileDialogFilePicker) ' Opens a File Exploer dialog box '

    With FileChoice ' This is used to set what is being displayed '

        .ButtonName = "Select"
        .AllowMultiSelect = True  ' Disables the abilty to select more than 1 file
        .InitialView = msoFileDialogViewDetails ' Sets the start location '
        .Show ' Allows it to be seen '

        ReDim WorkBookArray(.SelectedItems.Count, 1)

        For Each oFD In .SelectedItems ' This is used to create a file path used to open the file

            FilePath = oFD
            counter = counter + 1
            Set WorkBookArray(counter,1) = Workbook.Open(FilePath)

        Next oFD

End With

End Sub

2 个答案:

答案 0 :(得分:1)

不使用filedialog,但采用类似的方法,这就是我解决问题的方法

Public arrWorkbooks() As Excel.Workbook

Sub open_workbooks()

Dim strWorkbookPaths(2) As String
Dim intCounter As Integer

strWorkbookPaths(0) = "C:\Workspace\Dummy Data\test1.xlsx"
strWorkbookPaths(1) = "C:\Workspace\Dummy Data\test2.xlsx"
strWorkbookPaths(2) = "C:\Workspace\Dummy Data\test3.xlsx"

ReDim arrWorkbooks(UBound(strWorkbookPaths))

For intCounter = 0 To UBound(strWorkbookPaths)

    Set arrWorkbooks(intCounter) = Workbooks.Open(strWorkbookPaths(intCounter))

Next intCounter

End Sub

答案 1 :(得分:0)

有一个

Workbook

必须是:

Workbooks

并且您不需要2D数组

所以你的代码可以如下:

Public WorkBookArray() As Excel.Workbook
Sub Channel_1()
     With Application.FileDialog(msoFileDialogFilePicker) ' Opens a File Exploer dialog box '
        .ButtonName = "Select"
        .AllowMultiSelect = True  ' Disables the abilty to select more than 1 file
        .InitialView = msoFileDialogViewDetails ' Sets the start location '
        .Show ' Allows it to be seen '

        ReDim WorkBookArray(1 To .SelectedItems.Count)

        For Each oFD In .SelectedItems ' This is used to create a file path used to open the file
            counter = counter + 1
            Set WorkBookArray(counter,1) = Workbooks.Open(oFD)
        Next 
     End With
End Sub

当然,只要这些工作簿中的任何一个被克隆,WorkBookArray()中的相应项就会成为空引用