我的代码有一些问题,因为它必须打开一个关闭的excel文件,它仅在名称短时才起作用,因为该文件会自动显示为“ OPEN ORDERS 16.05.2018”,我想无需重新命名文件OPEN_ORDER即可输入此文件,例如,它可以处理最后一个但不能使用长名。
Sub OOR()
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("\\data\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
编辑:
=INDEX(OPEN ORDERS '[16.07.2018]16.07'!$R:$R,MATCH(1,(E5=OPEN ORDERS '[16.07.2018]16.07'!$E:$E)*(J5=OPEN ORDERS '[16.07.2018]16.07'!$J:$J),0))
这是名称输入后出现的公式,当提示“ OPEN ORDERS 16.07.2018”时,我不知道为什么它会打开OPEN ORDERS '[16.07.2018]16.07
,空格和“。”可能存在问题但我不知道如何解决。
答案 0 :(得分:0)
Sub OOR()
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("\\data\Documents\" & mnt & ".xlsx")
ActiveWindow.Visible = False
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
ty = Array("=INDEX(" & mnt2 & "!$R:$R,MATCH(1,(E2=" & mnt2 & "!$E:$E)*(J2=" & mnt2 & "!$J:$J),0))")
ws.Range("R2:R" & lr).FormulaArray = ty
ThisWorkbook.UpdateLinks = xlUpdateLinksAlways
Application.DisplayAlerts = True
xWb.Close savechanges:=False
End Sub