Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果userform textbox值为空或字符串可以在范围列表中找到,如何退出sub_Excel_Vba - Fatal编程技术网

Excel 如果userform textbox值为空或字符串可以在范围列表中找到,如何退出sub

Excel 如果userform textbox值为空或字符串可以在范围列表中找到,如何退出sub,excel,vba,Excel,Vba,尽管我在网上找到了很多建议和变体,但我一直在努力找出答案。如果用户忘记输入值,或者可以在字符串范围内找到值,我想退出sub,这与userform有关 以下是我为IF子句尝试过的许多变体中的最新变体: Private Sub cmd_nProj_Click() Dim wb As Workbook Dim ws As Worksheet, ws_h As Worksheet Dim i_rc As Long Dim r_home As Range, r_ProdNa

尽管我在网上找到了很多建议和变体,但我一直在努力找出答案。如果用户忘记输入值,或者可以在字符串范围内找到值,我想退出sub,这与userform有关

以下是我为
IF
子句尝试过的许多变体中的最新变体:

Private Sub cmd_nProj_Click()
    Dim wb As Workbook
    Dim ws As Worksheet, ws_h As Worksheet
    Dim i_rc As Long
    Dim r_home As Range, r_ProdName As Range




    Set wb = Application.ThisWorkbook
    Set ws_h = wb.Sheets("Control")

    i_rc = ws_h.Cells(Rows.Count, 1).End(xlUp).Row

    Set r_home = ws_h.Range("A" & i_rc + 1)
    Set r_ProdName = ws_h.Range("N2:N30")
    If Me.tb_NewProjName = "" Or r_ProdName.Find(What:=Me.tb_NewProjName.Value, LookIn:=xlValues) Is Nothing Then
        MsgBox ("Either you have left the projection name blank or the projection name is already being used, please try again!")
        Exit Sub

    End If
end sub
其他变体我会得到一个带有block variable not set error的对象,在这个迭代中,if子句不能正常工作,即使textbox值出现在该范围内,if子句也会被跳过

提前谢谢

即使文本框值出现在该范围内,if子句也会被跳过

这是因为当您没有找到范围内的值时,您正在退出sub。 我添加了一个
Not
来解决这个问题

Private Sub cmd_nProj_Click()
    Dim wb As Workbook
    Dim ws As Worksheet, ws_h As Worksheet
    Dim i_rc As Long
    Dim r_home As Range, r_ProdName As Range




    Set wb = Application.ThisWorkbook
    Set ws_h = wb.Sheets("Control")

    i_rc = ws_h.Cells(Rows.Count, 1).End(xlUp).Row

    Set r_home = ws_h.Range("A" & i_rc + 1)
    Set r_ProdName = ws_h.Range("N2:N30")
    If Me.tb_NewProjName = "" Or Not (r_ProdName.Find(What:=Me.tb_NewProjName.Value, LookIn:=xlValues) Is Nothing) Then
        MsgBox ("Either you have left the projection name blank or the projection name is already being used, please try again!")
        Exit Sub

    End If
End Sub

哎呀!这是漫长的一天,哈哈。谢谢@Nad None