首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果用户表单文本框值为空或者可以在范围列表中找到字符串,如何退出sub

如果用户表单文本框值为空或者可以在范围列表中找到字符串,如何退出sub
EN

Stack Overflow用户
提问于 2019-02-21 03:30:53
回答 1查看 282关注 0票数 1

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

以下是我为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

其他变体我会得到一个块变量没有设置错误的对象,在这个迭代中,if子句不能正常工作,因为即使文本框值出现在该范围内,if子句也会被跳过。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-21 03:40:51

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

这是因为当您在范围中查找don‘t值时,您正在退出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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54793937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档