未填充VBA用户窗体上的目标文本框时出错

时间:2018-11-19 15:02:35

标签: excel vba excel-vba

提交来宾检查的详细信息时,我不需要填充搜索框,下面使用的代码是我的代码,是否可以添加其他代码来避免此错误。提前致谢!!!!! lblrow.Caption =。行'错误行

Private Sub Txtforename_Change()
    Dim rng As Range
    lblrow.Visible = False

    With Sheets("Report")
        Set rng = Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
    End With

    With rng.Find(Txtguestsearch, lookat:=xlWhole)
        lblrow.Caption = .Row
    End With
End Sub

1 个答案:

答案 0 :(得分:3)

您需要编写代码,以确保无法通过Range.Find方法找到您的值。 我还更新了您的LRow计算,以使用with块和更多标准calc

Private Sub Txtforename_Change()

Dim rng As Range, Found as Range
lblrow.Visible = False

With Sheets("Report")
    Set rng = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

Set Found = rng.Find(Txtguestsearch, lookat:=xlWhole)

If Not Found is Nothing Then
    With Found
        lblrow.Caption = .Row
    End With
Else                          '<--Optional
    MsgBox "Not Found"        '<--Optional
End If                      

End Sub