Skip to main content

如何在 Excel 中循環遍歷列直到找到特定值?

Author: Sun Last Modified: 2025-05-12

在某些情況下,您可能希望逐列循環遍歷,直到在工作表中找到特定值。這裡我介紹一個巨集代碼,幫助您循環遍歷數據,直到找到特定值。

循環遍歷直到特定值


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

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!