Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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
使用VBA检查某个值是否在某个范围内_Vba_Excel - Fatal编程技术网

使用VBA检查某个值是否在某个范围内

使用VBA检查某个值是否在某个范围内,vba,excel,Vba,Excel,我想检查一个值是否存在于某个范围内。如果它不在那里,那么我希望它跳转到WriteProcess,否则我希望它给出一个消息框,说明它存在并退出子进程 这是密码 'Write the Selected Value in the Range - Next Available row in the Column of Source For i = TableStartingRow + 1 To AddNewEntrow If Range(EntryColL

我想检查一个值是否存在于某个范围内。如果它不在那里,那么我希望它跳转到
WriteProcess
,否则我希望它给出一个消息框,说明它存在并退出子进程

这是密码

    'Write the Selected Value in the Range - Next Available row in the Column of Source
    For i = TableStartingRow + 1 To AddNewEntrow        
        If Range(EntryColLett & i).Value = wb21Tool.Sheets("Home").ComboBox1.Value Then
            MsgBox "The data exists in the Table"
            GoTo StopSub       
        Else
            GoTo WriteProcess
        End If
    Next

WriteProcess:
    wbTool.Sheets("Home").Range(EntryColLett & AddNewEntrow).Value = wb21Tool.Sheets("Home").ComboBox1.Value

StopSub:   
    'Turn on the ScreenUpdate
    Application.ScreenUpdating = True

请分享你的想法。谢谢。

您的问题是,如果循环过期(耗尽所有迭代),则无法控制它进入写进程

这是使用
GoTo
语句的一个问题。最好将这些控制在最低限度。例如,虽然这并不是检查每一行,但这只是一个如何避免额外的
GoTo
的示例

    'Write the Selected Value in the Range - Next Available row in the Column of Source
    For i = TableStartingRow + 1 To AddNewEntrow        
        If Range(EntryColLett & i).Value = wb21Tool.Sheets("Home").ComboBox1.Value Then
            MsgBox "The data exists in the Table"
            GoTo StopSub       
        Else
            wbTool.Sheets("Home").Range(EntryColLett & AddNewEntrow).Value = wb21Tool.Sheets("Home").ComboBox1.Value
        End If
    Next

StopSub:   
    'Turn on the ScreenUpdate
    Application.ScreenUpdating = True
但是,对表数据进行蛮力迭代似乎是不必要的,如果需要检查表中的所有行,最好只使用
Find
方法

假设
EntryColLet
是表示列字母的字符串:

    Dim tblRange as Range
    Dim foundRow as Range
    Set tblRange = Range(EntryColLet & (TableStartingRow+1) & ":" & EntryColLet & AddNewEntRow)
    Set foundRow = tblRange.Find(wb21Tool.Sheets("Home").ComboBox1.Value)
    If foundRow Is Nothing Then
        'The value doesn't exist in the table, so do something
        '
    Else
        'The value exists already
        MsgBox "The data exists in the Table"
        GoTo StopSub
    End If

    'More code, if you have any...

StopSub:
    Application.ScreenUpdating = True
关于剩余的
GoTo
——如果在条件
之后没有更多的代码执行,如果foundRow为Nothing
,则可以删除整个
Else
子句和
GoTo
标签:

    Dim tblRange as Range
    Dim foundRow as Range
    Set tblRange = Range(EntryColLet & (TableStartingRow+1) & ":" & EntryColLet & AddNewEntRow)
    Set foundRow = tblRange.Find(wb21Tool.Sheets("Home").ComboBox1.Value)
    If foundRow Is Nothing Then
        'The value doesn't exist in the table, so do something
    End If
    Application.ScreenUpdating = True
End Sub

如果您需要在执行“WriteProcess”之前检查每一行,则可以选择其他解决方案:


这里有问题吗?代码片段看起来可以正常工作。您是否遇到错误?似乎最好使用range
Find
方法或
应用程序。Match
函数,但我看不出这段代码有什么问题。请详细说明。@tigeravatar是的,先生。它很好用。但这不是我想要它做的。当我在调试模式下运行代码时,我发现它在运行增量I中的所有值之前先运行else循环。我想让代码检查增量I中的所有值,只有当它在所有这些值中都不匹配时,它才必须转到WriteProcess。@DavidZemens说I的值为1,2,3。当i=1时,它为循环运行,当它不匹配时,它进入写入过程。我希望它一直运行到3,当它不匹配时,它需要转到写入进程,如果在此之前找到了匹配项。它必须给出消息框并转到stopsub。如果您想在转到
stopsub
之前遍历所有
i
值,为什么不直接取出
Else
部分,并将
转到stopsub
放在
下一个
之后?(虽然你真的不应该使用
GoTo
,但那是一个不同的主题…)现在,对于每个
i
值,如果
…值不相等,它将转到
StopSub
行…我明白了,我错在哪里,先生。谢谢但我的要求仍然不符合。谢谢。我完全同意你的建议,使用
Range.Find
方法比遍历每个单元格要好得多
    Dim bExists As Boolean

    bExists = False
    'Write the Selected Value in the Range - Next Available row in the Column of Source
    For i = TableStartingRow + 1 To AddNewEntrow
        If Range(EntryColLett & i).Value = wb21Tool.Sheets("Home").ComboBox1.Value Then
            bExists = True
            MsgBox "The data exists in the Table"
            Exit For
        End If
    Next

    If Not bExists Then wbTool.Sheets("Home").Range(EntryColLett & AddNewEntrow).Value = wb21Tool.Sheets("Home").ComboBox1.Value

    'Turn on the ScreenUpdate
    Application.ScreenUpdating = True