简体   繁体   中英

to open workbook from an array

this is my code where users can select multiple files and then they are compared with headers in master file and then data is copy pasted. the problem is that i do not know how to reference the workbooks from the array to run the code, one workbook at a time. previously for a single workbook i used the activate statement but i do not know how to do it for multiple workbooks in the array. the book names are stored in arrNames. Temp calc is the sheet where all the data has to be stored. any suggestions ?

thanks,

Mathew

Sub Test()

     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

Here i need the code to open workbook in array

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

you could try

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

where i is your loop counter through the array returned by GetOpenFileName and arrNames is your array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM