简体   繁体   English

遍历范围和检查值(Excel VBA)

[英]Looping through range and check value (Excel VBA)

I have written a simple VBA code to loop through a range to check for a particular value, if it is true, then it would switch a boolean trigger to False. 我编写了一个简单的VBA代码,以遍历一个范围来检查特定值,如果为true,则它将布尔触发器切换为False。

However, my code does not work. 但是,我的代码不起作用。 Even though I have placed '12345' somewhere in the range, it still returns Infochk = True. 即使我将“ 12345”放置在范围内的某个位置,它仍然返回Infochk = True。

Code

Dim InfoChk As Boolean
InfoChk = True
LastRow = Sheets("Sheet1").Cells(Rows.count, "B").End(xlUp).row
For i = 7 To LR
    With Worksheets("Sheet1")
        If .Cells(i, 6).Value = "12345" Then
            MsgBox ("gotcha")
            InfoChk = False
            Exit For
        End If
    End With
Next i
MsgBox (InfoChk)

Any help would be greatly appreciated. 任何帮助将不胜感激。

Use LastRow: 使用LastRow:

Sub poiuy()
    Dim InfoChk As Boolean
    InfoChk = True
    LastRow = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
    For i = 7 To LastRow
        With Worksheets("Sheet1")
            If .Cells(i, 6).Value = "12345" Then
                MsgBox ("gotcha")
                InfoChk = False
                Exit For
            End If
        End With
    Next i
    MsgBox (InfoChk)
End Sub

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM