这是我的代码,用户可以选择多个文件,然后将它们与主文件中的标题进行比较,然后复制粘贴数据。问题是我不知道如何从数组中引用工作簿来运行代码,一次一个工作簿。以前对于单个工作簿我使用了activate语句,但我不知道如何为数组中的多个工作簿执行此操作。书名存储在arrNames中。临时计算是必须存储所有数据的工作表。有什么建议 ?
感谢,
马修
子测试()
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
答案 0 :(得分:1)
你可以尝试
sPath="C:\"
workbooks(sPath & arrNames(i)).open
其中 i 是您通过GetOpenFileName
返回的数组的循环计数器, arrNames 是您的数组