使用“查找”功能找不到的值的错误消息

时间:2018-09-11 02:33:49

标签: excel vba excel-vba

我正在使用此代码来检查条形码是否在数据库中,但是每次条形码不在列表中时,都会提示错误消息:

  

运行时错误91:未设置对象变量或With块变量。

我是否可以添加一行,例如msgbox,所以输入的条形码无效。我知道这是我所需要的,但是显然,我不知道应该使用IF语句使用哪个函数。有什么建议吗?

如果有人可以建议使用FOR语句(如果要搜索一批),我也将不胜感激。 1111-1114

Private Sub CheckBarcodeStatusCommandButton_Click()
    ActiveWorkbook.Sheets("Inventory Log").Select
    Columns("J:J").Select
    Selection.Find(What:=CheckBarcodeTextBox.Text, after:=ActiveCell, _
        LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, MatchByte:=False, _
        SearchFormat:=False).Activate

    If ActiveCell.Offset(0, 1).Value = "In" Then
        MsgBox ("The barcode no. " & CheckBarcodeTextBox.Text _
            & " is currently available")
    ElseIf ActiveCell.Offset(0, 1).Value = "Out" Then
        MsgBox ("The barcode no. " & CheckBarcodeTextBox.Text _
            & " has already been used.")
    End If

    Application.DisplayAlerts = False
End Sub

3 个答案:

答案 0 :(得分:3)

来自documentation

  

如果未找到匹配项,则此方法返回Nothing.Find方法不会影响选择或活动单元格。


这演示了如何使用VBA .Find方法:

(根据您的示例)这假设CheckBarcodeTextBox包含要在列J中匹配的文本,仅匹配“整个单元格”。

Private Sub CheckBarcodeStatusCommandButton_Click()
    Dim lookFor As String, lookIn As Range, found As Range

    lookFor = CheckBarcodeTextBox.Text
    Set lookIn = ThisWorkbook.Sheets("Inventory Log").Columns("J:J")
    Set found = lookIn.Find(lookFor, , xlValues, xlWhole) 'match whole cell value

    If found Is Nothing Then
        'not found
        MsgBox "No match for: " & lookFor
    Else
        'found
        MsgBox "Found: " & lookFor & vbLf & _
               " in cell: " & found.Address & vbLf & _
               " which contains: " & found.Value
    End If
End Sub

如果您只需要检查匹配项是否存在 (并且不需要知道匹配项的位置),则可以简化上面的示例,位。

Private Sub CheckBarcodeStatusCommandButton_Click()
    Dim lookIn As Range
    Set lookIn = ThisWorkbook.Sheets("Inventory Log").Columns("J")
    If lookIn.Find(CheckBarcodeTextBox, , xlValues, xlWhole) Is Nothing Then
        MsgBox "Not found:."    'do something if not found
    Else
        MsgBox "Found."         'do something if found
    End If
End Sub

在工作表公式中,我将使用VLOOKUPMATCH,可以使用Application.WorksheetFunction来调用它们,但是两者都需要On Error处理才能处理不匹配项,因此.Find可能是最好的。


来自Microsoft文档的更多信息:

推荐的书签 Microsoft文档:Office VBA Reference
使用网站的左侧边栏导航至VBA functionsmethodsstatements之类的部分。

答案 1 :(得分:1)

尝试

Private Sub CheckBarcodeStatusCommandButton_Click()
    dim c as range

    ActiveWorkbook.Sheets("Inventory Log").Columns("J:J").Select
    on error resume next
    set c = Selection.Find(What:=CheckBarcodeTextBox.Text, after:=ActiveCell, _
        LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, MatchByte:=False, _
        SearchFormat:=False)

    if c is nothing then 
        msgbox "barcode not found."
    else
        c.Activate

        If ActiveCell.Offset(0, 1).Value = "In" Then
            MsgBox ("The barcode no. " & CheckBarcodeTextBox.Text _
                & " is currently available")
        ElseIf ActiveCell.Offset(0, 1).Value = "Out" Then
            MsgBox ("The barcode no. " & CheckBarcodeTextBox.Text _
                & " has already been used.")
        End If
    end if
    on error goto 0
    Application.DisplayAlerts = False
End Sub

答案 2 :(得分:1)

工作表的MATCH与Range.Find一样快或更快。使用返回到变体的Application.Match,可以使用IsError进行测试。

generate_pattern