跳至主要内容

如何在Excel中循环遍历行直到找到特定值?

Author: Sun Last Modified: 2025-07-31

在某些情况下,您可能希望逐行循环遍历,直到在工作表中找到特定值。这里我介绍一个宏代码,帮助您循环遍历数据,直到找到特定值。

循环遍历直到找到特定值


arrow blue right bubble 循环遍历直到找到特定值

1. 按 Alt + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 单击 插入 > 模块,将以下代码粘贴到空白 模块 中。

VBA:循环遍历行直到找到特定值

Sub LoopUntilSpecificValue()
'UpdatebyExtendoffice20161222
    Dim fStr As String
    Dim strActAddress As String
    Dim fbool As Boolean
    Dim cnt As Long, cntRow As Long
    Dim xRg As Range, yRg As Range, zRg As Range
    fbool = False
    strActAddress = ""
    cnt = 0
    On Error Resume Next
    Set zRg = ActiveSheet.UsedRange
    cntRow = zRg.Rows.Count
   
    Set xRg = Application.InputBox _
        (Prompt:="Range select..", Title:="Kutools for Excel", Type:=8)
    fStr = Application.InputBox _
        (Prompt:="Search string?", Title:="Kutools for Excel", Type:=2)
    Application.ScreenUpdating = False
    For Each yRg In xRg
       
        If yRg.Row > cntRow Then
            MsgBox "Value not found ", vbInformation, "Kutools for Excel"
            Application.ScreenUpdating = True
            Exit Sub
        End If
        If yRg.Value2 = fStr Then
            Application.ScreenUpdating = True
            yRg.Activate
            fbool = True
            strActAddress = yRg.Address
            MsgBox "Value found in cell " & strActAddress, vbInformation, "Kutools for Excel"
            
            Exit Sub
        Else
            cnt = cnt + 1
        End If
        
    Next yRg
    
    If cnt = xRg.Count Then
        MsgBox "Value not found ", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = True
End Sub

A screenshot showing the pasted code in the module window

3. 按下 F5 键,将弹出一个对话框供您选择要循环遍历的列。参见截图:
A screenshot showing the input box for selecting a range to loop through in Excel

4. 单击 确定,然后在第二个弹出的对话框中输入您要查找的字符串。参见截图:
A screenshot of the input box for entering the string to search for

5. 单击“确定”。它将循环遍历选中的区域,直到找到指定的值。
A screenshot of the dialog showing the loop search result in Excel

备注:如果未找到匹配值,将出现一个对话框通知您未找到匹配项。
A screenshot showing the dialog box notifying no value found in Excel

最佳 Office 办公效率工具

🤖 Kutools AI 助手:基于智能执行,彻底革新数据分析 生成代码创建自定义公式分析数据并生成图表调用 Kutools Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!