大家好,我为两个文件之间的索引匹配编写了一个代码,但是我遇到了一些麻烦,因为输入每个单元格的文件名后,一个窗口会弹出来更新值(选择我拥有的excel文件)来获取数据)
Sub cacontinue2()
Dim ws As Worksheet
Set ws = Sheets("Sheet 1")
Dim lr As Long
Dim mnt As String, mnt2 As String
mnt = InputBox("Filename")
mnt2 = "H:\Documents\" & mnt & ".xlsx"
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
ty = Array("=INDEX(mnt!$R:$R,MATCH(1,(E2=mnt!$E:$E)*(J2=mnt!$J:$J),0))")
ws.Range("R2:R" & lr).FormulaArray = ty
End Sub
答案 0 :(得分:1)
进行棘手的测试,无需花费大量时间来模拟工作簿,但这应该可以工作-请注意:您需要包括您要使用的标签/工作表名称重新指向-您不能忽略它。
外部未打开文件的“地址”应采用以下格式:
='PATH_TO_FOLDER\[FILENAME]SHEETNAME'!ADDRESS
Sub cacontinue2()
Dim ws As Worksheet
Set ws = Sheets("Sheet 1")
Dim lr As Long
Dim mnt As String, mnt2 As String
mnt = InputBox("Filename")
mnt2 = "'H:\Documents\[" & mnt & ".xlsx]Sheet1'" ' Change Sheet1 to the actual tabname
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
ty = "=INDEX(" & mnt2 & "!$R:$R,MATCH(1,(E2=" & mnt2 & "!$E:$E)*(J2=" & mnt2 & "!$J:$J),0))"
ws.Range("R2:R" & lr).FormulaArray = ty
End Sub
注意:如果文件夹或文件名包含空格,我将mnt2
包裹在'
中。
注意:如果要一次写入等效的数组公式,请使用自动填充完成
Sub cacontinue2()
Dim ws As Worksheet
Set ws = Sheets("Sheet 1")
Dim lr As Long
Dim mnt As String, mnt2 As String
mnt = InputBox("Filename")
mnt2 = "'H:\Documents\[" & mnt & ".xlsx]Sheet1'" ' Change Sheet1 to the actual tabname
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
ty = "=INDEX(" & mnt2 & "!$R:$R,MATCH(1,(E2=" & mnt2 & "!$E:$E)*(J2=" & mnt2 & "!$J:$J),0))"
ws.Range("R2").FormulaArray = ty
ws.Range("R2").autofill Destination:=ws.Range("R2:R" & lr)
End Sub
答案 1 :(得分:0)
谢谢大家!在此工作后,如果文件关闭,我将遇到数组过长的问题(不能超过255个字符),所以我放了一些东西来打开文件并在之后关闭它。
Sub cacontinue100()
Dim ws As Worksheet
Set ws = Sheets("Sheet 1")
Dim lr As Long
Dim mnt As String, mnt2 As String
Dim xWb As Workbook
ThisWorkbook.UpdateLinks = xlUpdateLinksNever
Application.DisplayAlerts = False
mnt = InputBox("Filename")
mnt2 = "'H:\Documents\[" & mnt & ".xlsx]Sheet 1'" ' Change Sheet1 to the actual tabname
Set xWb = Workbooks.Open("\\klxdata\homeshares\klxaero\nbi\Documents\" & mnt & ".xlsx")
ActiveWindow.Visible = False
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
ty = Array("=INDEX(" & mnt & "!$R:$R,MATCH(1,(E2=" & mnt & "!$E:$E)*(J2=" & mnt & "!$J:$J),0))")
ws.Range("R2:R" & lr).FormulaArray = ty
ThisWorkbook.UpdateLinks = xlUpdateLinksAlways
Application.DisplayAlerts = True
xWb.Close savechanges:=False
End Sub
这可能远非最佳,但有效