424对象VBA Vlookup需要的错误

我正在通过下面的脚本,以最终从一个数据查询到一个单独的工作表的数据范围内,根据inputinputboxinput的值,但是我不断遇到一个424错误 – 所需的对象。

要testing这是否工作,我正在尝试显示一个消息框,以便我可以开始下一个阶段,但是脚本在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") 

请有人提供一个答案,为什么发生这种情况,并希望解决这个问题。 先谢谢你!

在引用工作表之前,尝试添加Set TRELINFO = Sheets.("Sheet1") 。 用您的TRELINFO表replace'Sheet1'。

编辑:如果您的表的第一行中没有与用户input匹配的值,则VLOOKUP将返回一个错误。 在excel中testing一个表格(使用date格式)后,下面的工作。

 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 

NextRelease是一个string值:如果没有先将其转换为Double,则无法在date表中查看。

既然你没有从VLOOKUP返回值,你可以更简单地使用MATCH。

通过删除WorksheetFunction ,您可以testing错误的返回值,而不会在没有匹配时提高运行时错误。

 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") 

确保工作表TRELINFO存在或被正确命名。 看来该function找不到该工作表。