我正在使用以下脚本,最终根据 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")
有人可以提供一个答案,说明为什么会这样,希望能解决这个问题。 提前谢谢!
答案 0 :(得分: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")
答案 1 :(得分:0)
确保TRELINFO表存在或命名正确。看来该功能无法找到该工作表。
答案 2 :(得分: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
答案 3 :(得分:0)
请改用
ReleaseDate = Application.VLookup(NextRelease, Worksheets("trelinfo").Range("A2:B4"), 1, False)
在使用VLookUp函数之前我遇到了这个错误。我只是删除WorksheetFunction然后它工作