【问题标题】:Error 1004 application-defined or object-defined error using vlookup?使用 vlookup 的错误 1004 应用程序定义或对象定义错误?
【发布时间】:2017-10-24 04:46:23
【问题描述】:

我在尝试使用此 vlookup 时收到此错误 1004。我使用一个窗口来选择一个文件,然后在 vlookup 中使用该文件。我在我拥有的另一个宏中执行此操作,并且使用了基本相同的代码。但由于某种原因,这个不起作用。任何人都可以看到任何明显的问题吗?我无法弄清楚我做错了什么。

我在“With ws”之后的第一个 VLOOKUP 公式出现错误

Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String
Dim shtName As String


' Prompt
strPrompt = "Please select the last Kronos Full File before the dates of this Report." & vbCrLf & _
    "For example, if the date of this report is 9-8-17, you would want to use the closest date Kronos Full File." & vbCrLf & _
    "If one was not ran in the past couple days, then run a new Kronos Full File, and then choose that file."

' Dialog's Title
strTitle = "Latest Kronos Full File"

'Display MessageBox
iRet = MsgBox(strPrompt, vbOK, strTitle)

Dim Window2 As String
Dim X As String
Dim lNewBracketLocation As Long
Dim wb2 As Workbook


Window2 = Application.GetOpenFilename( _
    FileFilter:="Excel Files (*.xls*),*.xls*", _
    Title:="Choose the Newest Kronos Full File", MultiSelect:=False)

Set wb2 = Workbooks.Open(Filename:=Window2, ReadOnly:=True)
shtName = wb2.Worksheets(1).name
wb2.Close

MsgBox "You selected " & Window2
'Find the last instance in the string of the path separator "\"
lNewBracketLocation = InStrRev(Window2, Application.PathSeparator)
'Edit the string to suit the VLOOKUP formula - insert "["
X = Left$(Window2, lNewBracketLocation) & "[" & Right$(Window2, Len(Window2) - lNewBracketLocation)


With ws
.Range("M2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$E$99999,4,0)"
.Range("N2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$C$99999,2,0)"
.Range("O2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$U$99999,20,0)"
.Range("P2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$Q$99999,16,0)"
.Range("Q2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$S$99999,18,0)"
End With

【问题讨论】:

  • 你确定 x = 你认为应该的吗?
  • 好问题我猜这可能就是它的样子,但我从我的其他代码中复制了它并且这些代码有效。我会再看一遍。
  • 你不需要.Range("M2").Formula = "=VLOOKUP($K2,'" & X & "]" & shtName & "'!$B$2:$E$99999,4,0)" 因为shtName 是一个变量吗?
  • 嗯,是的,现在当我去调试时,会弹出 shtName 作为实际工作表名称现在应该是什么。但我仍然收到错误
  • @Robillard 不!调试器是你的主要工具,学会使用它!您可以使用 F9 在任何可执行语句上切换断点。当你运行代码时,它会停在那里。然后您可以按 F8 逐行运行代码,使用 立即窗格 (Ctrl+G) 来测试并动态重新分配内存中的值,使用 Locals i> 用于查看当前范围内所有内容的值的工具窗口,...不要只是清除代码并重写它,学习使用调试器,这是基础!

标签: vba excel


【解决方案1】:

使用另一个工作簿中的Range 的地址的另一种方法是设置范围,稍后您可以使用Range.Address(True, True, xlR1C1, xlExternal)。第 4 部分将根据需要添加工作表和工作簿的名称。

Dim Rng1 As Range  ' new Range Object

Window2 = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*),*.xls*", _
                                Title:="Choose the Newest Kronos Full File", MultiSelect:=False)

Set wb2 = Workbooks.Open(Filename:=Window2, ReadOnly:=True)
'shtName = wb2.Worksheets(1).Name '<-- not necessary

Set Rng1 = wb2.Worksheets(1).Range("B2:E99999")    
wb2.Close

With ws
    .Range("M2").Formula = "=VLOOKUP($K2," & Rng1.Address(True, True, xlR1C1, xlExternal) & ",4,0)"
    ' define more ranges for the other formulas

End With

【讨论】:

    【解决方案2】:

    似乎我的问题与我尝试使用 VLOOKUP 的范围有关。看起来一旦我将 99999 更改为仅像 9999,那么 VLOOKUP 似乎工作了。我仍然不确定为什么,但我很确定就是这样。当我降低该数字范围时,我没有收到错误消息。我猜是因为它超出了实际工作表或其他东西的范围。

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 2023-03-28
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多