Site icon JAFN's note

【教學】Excel VBA 如何使用Find查找值並回傳位置?超簡單馬上搞定!

excel vba find 完全符合 搜尋關鍵字

Step 1 在功能列「開發人員」中程式碼選擇「Visual Basic」


Step 2 複製下方程式碼貼上,並修改尋找的值以及尋找範圍後,按下執行

Sub FindValue()
    Dim rng As Range
    Dim searchValue As String
    Dim foundCell As Range
    
    ' 要尋找的值
    searchValue = "test"
    
    ' 在A欄及B欄中尋找
    Set rng = Range("A:B")
    
    ' 使用 Find 方法尋找值
    Set foundCell = rng.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
    
    ' 如果找到,則顯示其地址
    If Not foundCell Is Nothing Then
        MsgBox "找到值 " & searchValue & " 在儲存格 " & foundCell.Address
    Else
        MsgBox "找不到值 " & searchValue
    End If
End Sub
Range.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)

Step 3 即可回傳關鍵字值的位置

【教學】Excel VBA 如何使用Find查找值並回傳位置?超簡單馬上搞定!
Exit mobile version