【问题标题】:input box for opening file works only with short names打开文件的输入框仅适用于短名称
【发布时间】:2018-07-23 12:47:58
【问题描述】:

我的代码有一些问题,因为它必须打开一个封闭的 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 可能是空格和“。”有问题。但我不知道如何解决它。

【问题讨论】:

  • 欢迎来到 Stack Overflow:请阅读 How to ask a good question,然后编辑您的问题并确保询问 good, clear, concise question,包括代码、预期行为以及问题所在......然后我们可以尝试帮助
  • 问题不在于长度。它是文件名中的.
  • @Basho:那么你的代码哪里失败了?你试过F8 Debugging吗?
  • @Basho:请参阅此related question on StackOverflow 在那里您会找到一些帮助
  • Workbooks.Open("\\data\Documents\" & mnt - 你需要完整的文件夹地址

标签: excel vba


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2022-09-29
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多