2

这是我的代码,用户可以选择多个文件,然后将它们与主文件中的标题进行比较,然后复制粘贴数据。问题是我不知道如何引用数组中的工作簿来运行代码,一次一个工作簿。以前对于单个工作簿,我使用了激活语句,但我不知道如何为数组中的多个工作簿执行此操作。书名存储在 arrNames 中。Temp calc 是必须存储所有数据的工作表。有什么建议么 ?

谢谢,

马修

子测试()

     Dim lastCol, lastRow As Long, k As Long, a As Variant, b As Variant, cmpRng As Range
    Dim mastCol As Long, mastRng As Range, n As Long
    Dim Wbk As Workbook
    Dim fileone
     Dim SelectedFiles As Object
     Dim arrNames As Variant
     Dim indx As Long

    Application.ScreenUpdating = False

Sheets("Temp Calc").Select
'Clear existing sheet data except headers
    Rows(1).Offset(1, 0).Resize(Rows.Count - 1).ClearContents


    arrNames = Application.GetOpenFilename(Filefilter:="Workbooks (*.xlsx),*.xlsx", MultiSelect:=True)
       For i = 1 To UBound(arrNames, 1)


    Worksheets("Temp Calc").Select

       lastCol = Worksheets("Temp Calc").Cells(1, Columns.Count).End(xlToLeft).Column
       lastRow = Worksheets("Temp Calc").Cells(Rows.Count, 1).End(xlDown).Row

    Set cmpRng = Range(Cells(1, 1), Cells(1, lastCol))
    a = cmpRng
    mastCol = Cells(1, Columns.Count).End(xlToLeft).Column

    Set mastRng = Range(Cells(1, 1), Cells(1, mastCol))
    b = mastRng

    For k = 1 To lastCol
        For n = 1 To mastCol
            If UCase(a(1, k)) = UCase(b(1, n)) Then

在这里我需要代码来打开数组中的工作簿

Worksheets("Sheet1").Range(Cells(2, n), Cells(lastRow, n)).Copy
                Windows("Dashboard_for_Roshan.xlsm").Activate
                Worksheets("Temp Calc").Select
                Cells(2, k).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                False, Transpose:=False

                Exit For
            End If
        Next
    Next


        Next

    'Else

    'End If

    Application.ScreenUpdating = True

    Exit Sub
        'Next
End Sub
4

1 回答 1

1

你可以试试

sPath="C:\"
workbooks(sPath & arrNames(i)).open

其中i是您通过返回的数组的循环计数器,GetOpenFileNamearrNames是您的数组

于 2013-06-26T12:34:35.467 回答