首页 > 解决方案 > 循环遍历范围和检查值(Excel VBA)

问题描述

我编写了一个简单的 VBA 代码来循环遍历一个范围以检查特定值,如果它是真的,那么它将一个布尔触发器切换为假。

但是,我的代码不起作用。即使我在范围内的某个位置放置了“12345”,它仍然返回 Infochk = True。

代码

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)

任何帮助将不胜感激。

标签: vbaexcel

解决方案


使用最后一行:

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

在此处输入图像描述


推荐阅读