公式过长,仅适用于一个单元格

时间:2018-07-23 06:59:39

标签: excel vba excel-vba

嘿,我的INDEX MATCH公式遇到了一些麻烦,

Sub AAAAAAAAAAAAAAHHHHHHH()


    Dim ws As Worksheet
    Set ws = Sheets("Sheet 1")
    Dim lr As Long
    lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
    Range("R2:R" & lr).FormulaArray = "=INDEX('\\data\Documents\[OPEN ORDERS 16.07.2018.xlsx]Sheet 1'!$R:$R,MATCH(1,(E2='\\data\Documents\[OPEN ORDERS 16.07.2018.xlsx]Sheet 1'!$E:$E)*(J2='\\data\Documents\[OPEN ORDERS 16.07.2018.xlsx]Sheet 1'!$J:$J),0))"
End Sub

我遇到两个问题,第一个问题是INDEX公式太长,因此它不起作用,另一个问题是该公式仅适用于E2而不适用于E3; E4;等等  我曾想过可能通过将名称更改为变量来缩短它,以便每周更改名称会更容易。 在此先感谢您的帮助!

Sub AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHH()
Dim ws As Worksheet
Set ws = Sheets("Sheet 1")
Dim lr As Long

lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
Range("R2:R" & lr).FormulaArray = "=INDEX('H:\Documents\[OPEN ORDERS 16.07.2018.xlsx]Sheet 1'!$R:$R,MATCH(1,(E2='H:\Documents\[OPEN ORDERS 16.07.2018.xlsx]Sheet 1'!$E:$E)*(J2='H:\Documents\[OPEN ORDERS 16.07.2018.xlsx]Sheet 1'!$J:$J),0))"

End Sub

我已经更改了路径,所以现在还不算太长,我只需要找到一种方法即可根据行将其应用于每个单元格,因为现在它只放置E2和J2的结果,所以我有同样的想法我所有的R列

   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 = ("=INDEX(mnt!$R:$R,MATCH(1,(E2=mnt!$E:$E)*(J2=mnt!$J:$J),0))")
ws.Range("R2:R" & lr).FormulaArray = ty

现在我为每个值打开了一个更新值窗口

1 个答案:

答案 0 :(得分:0)

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