我需要使用Excel VBA搜索一系列單元格,返回第一個匹配的行號。只要我正在搜索值,使用Match函數將很容易。但是我需要搜索公式,而不是價值。Excel 2010+ VBA - 如何搜索範圍的公式而不是值
例如:I需要VBA返回 「4」 當我搜索 「= A4 + 2」 ...
我需要使用Excel VBA搜索一系列單元格,返回第一個匹配的行號。只要我正在搜索值,使用Match函數將很容易。但是我需要搜索公式,而不是價值。Excel 2010+ VBA - 如何搜索範圍的公式而不是值
例如:I需要VBA返回 「4」 當我搜索 「= A4 + 2」 ...
,你可以直接與match
Application.Match("=A4+2", Range("B1:B5").Formula)
這樣做會給你4
編輯
你可能會得到CUS 255個字符立的錯誤mit來自Match
。您也可以使用工作表內的輸出。簡單地把這個代碼放在模塊中:
Public Function MATCHFUNC(str As String, rng As Range, Optional fOnly As Boolean, Optional fAdr As Boolean) As Variant
Dim i As Long, runner As Variant
If UBound(rng.Value, 1) > 1 And UBound(rng.Value, 2) > 1 And Not fAdr Then MATCHFUNC = 0: Exit Function
For Each runner In rng
i = i + 1
If Not fOnly Or (runner.Text <> runner.Formula) Then
If InStr(1, runner.Formula, str, 1) Then
If fAdr Then MATCHFUNC = runner.Address Else MATCHFUNC = i
Exit Function
End If
End If
Next
MATCHFUNC = 0
End Function
你現在可以像使用普通的工作表一樣使用它。作爲例子與您的圖片:
MATCHFUNC([string to search for],[range to look in],[1 to look only in cells containing formulas],[1 to get the address in $A$1 format])
=MATCHFUNC("+2",B3:B5) = 1 - it was found in the first cell
=MATCHFUNC("2",B1:B5) = 2 - "2" is also in B2
=MATCHFUNC("2",B1:B5,1) = 3 - B2 will be skipped - formulas only
=MATCHFUNC("+2",B3:B5,,1) = "$B$3" - address of the first cell with match
=MATCHFUNC("9",B1:B5) = 0 - not found in range
=MATCHFUNC("2",A1:B5) = 0 - range needs to be only 1 row or 1 column without fAdr
=MATCHFUNC("2",A1:B5,,1) = "$B$2" - check goes A1->B1...->A2->B2...
您可能需要使用fAdr = 1
特殊情況類似:
=ROW(INDIRECT(MATCHFUNC("2",B4:B5,,1))) = 4 - absolute row of the first cell with match
Asuming你不想檢查B1:無論出於何種原因B3但你需要絕對的行。
不過你也可以在VBA本身使用它像:iVal = MATCHFUNC("=B", Range("B4:B5"))
同樣的功能本身可以easiely改善,也輸出數組或一個運行檢查不同的字符串或做任何你想要的(如果沒有必要,你也可以跳過2個選項部分,以保持它的速度和易於理解):)
如果你想要第一個找到的單元格的地址 - 這將作爲工作表函數(=FindFirst("=A",B2:B6)
),並從另一個VBA程序:
Public Function FindFirst(FindValue As String, InRange As Range) As Variant
Dim rFound As Range
With InRange
Set rFound = .Find(_
What:=FindValue, _
After:=InRange.Cells(InRange.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart)
If Not rFound Is Nothing Then
FindFirst = rFound.Address
Else
FindFirst = CVErr(xlErrValue)
End If
End With
End Function
另一方面,如果你想要所有找到的單元格,你可以使用它 - 但是請注意,它不能用作工作表函數。
Public Sub Test()
MsgBox FindInFormula("=A", ThisWorkbook.Worksheets("Sheet1").Range("B2:B6")).Address
End Sub
Public Function FindInFormula(FindValue As String, InRange As Range) As Range
Dim rFound As Range
Dim sFirstAdd As String
Dim rReturnRange As Range
With InRange
Set rFound = .Find(_
What:=FindValue, _
After:=InRange.Cells(InRange.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart)
If Not rFound Is Nothing Then
sFirstAdd = rFound.Address
Do
If rReturnRange Is Nothing Then
Set rReturnRange = rFound
Else
Set rReturnRange = Union(rReturnRange, rFound)
End If
Set rFound = .FindNext(rFound)
Loop While Not rFound Is Nothing And rFound.Address <> sFirstAdd
End If
End With
Set FindInFormula = rReturnRange
End Function
您將需要更新的程序返回地址或到單元格的引用 - 調整您的需求。
哇...正是我所需要的。我錯過了「.Formula」屬性。非常感謝。 –
不用客氣 –
那真是太棒了,還有我需要記住的東西! – sous2817