424对象VBA Vlookup所需的错误

问题描述 投票:1回答:4

我正在通过以下脚本最终根据inputbox中输入的值将数据查询中的一系列数据拉到单独的工作表中,但是我一直遇到424错误 - 需要对象。

为了测试这是否正常,我正在尝试显示一个消息框,这样我就可以开始下一个阶段,但是脚本在Vlookup上一直失败

Private Sub Workbook_Open()


Dim NextRelease As String

If MsgBox("Would you like to promote the next release in batch?", vbQuestion + vbYesNo, "Promote the next release?") = vbYes Then
NextRelease = InputBox("Please enter the date of the next release", "Next Release", "DD/MM/YYYY")
ReleaseDate = Application.WorksheetFunction.VLookup(NextRelease, TRELINFO.Range("A2:B4"), 1, False)
If NextRelease = ReleaseDate Then
MsgBox ("Working")

有人可以提供一个答案,说明为什么会这样,并希望解决这个问题。先感谢您!

excel vba excel-vba vlookup
4个回答
0
投票

在参考工作表之前尝试添加Set TRELINFO = Sheets.("Sheet1")。用您的TRELINFO表单替换'Sheet1'。

编辑:如果表的第一行中没有与用户输入匹配的值,VLOOKUP将返回错误。在excel中对表(具有日期格式)进行测试后,下面适用于我。

Private Sub Workbook_Open()

Set trelinfo = Sheets("TRELINFO")
Dim NextRelease As Long

If MsgBox("Would you like to promote the next release in batch?", vbQuestion + vbYesNo, "Promote the next release?") = vbYes Then _
NextRelease = CLng(CDate(InputBox("Please enter the date of the next release", "Next Release", "DD/MM/YYYY")))

checkblank = WorksheetFunction.CountIf(trelinfo.Range("A2:A4"), NextRelease)

If checkblank <> 0 Then
    ReleaseDate = Application.WorksheetFunction.VLookup(NextRelease, trelinfo.Range("A2:B4"), 1, False)
    If NextRelease = ReleaseDate Then _
    MsgBox "Working"

Else
    MsgBox "Release not found"

End If

End Sub

1
投票

NextRelease是一个字符串值:如果没有先将其转换为Double,则无法在日期表中查找。

由于您没有从VLOOKUP返回值,因此您可以更简单地使用MATCH。

删除WorksheetFunction允许您测试错误的返回值,而不是在没有匹配时引发运行时错误。

Private Sub Workbook_Open()

Dim NextRelease As String

If MsgBox("Would you like to promote the next release in batch?", _
          vbQuestion + vbYesNo, "Promote the next release?") = vbYes Then

    NextRelease = InputBox("Please enter the date of the next release", _
                           "Next Release", "DD/MM/YYYY")

    If not IsError(Application.Match(CDbl(DateValue(NextRelease), _
                   TRELINFO.Range("A2:B4"), 0) Then

         MsgBox ("Working")

0
投票

确保TRELINFO表存在或命名正确。看来该功能无法找到该工作表。


0
投票

只需使用它

ReleaseDate = Application.VLookup(NextRelease, Worksheets("trelinfo").Range("A2:B4"), 1, False)

我在使用VLookUp函数之前遇到此错误。我只是删除WorksheetFunction然后它的工作原理

© www.soinside.com 2019 - 2024. All rights reserved.