[英]Partial string match and lookup in Excel-VBA
我對與 Excel 相關的事情感到很迷茫。
我需要在兩個表之間進行遞歸查找以找到匹配項,並在一個單元格中插入特定值。
在下一個示例中,我將向您展示應該做什么,我希望任何人都可以提供幫助! :)
基本上,我需要在每個“B”單元格中的“E”列中查找值,如果有任何匹配項,則在“C”中插入特定的“F”值。
我不知道一個公式是否足夠,但也許需要一個宏。
我將不勝感激任何幫助。
提前致謝!
=IFERROR(INDEX($F$1:$F$3,MATCH(1,--(COUNTIF($B1,"*"&$E$1:$E$3&"*")>0),0)),"No matching values")
將觸發范圍包裹在"*"
使其接受搜索字符串之前或之后的所有字符
使用字典試試這個 UDF。 如果未找到匹配項,它將返回空白。 它不是解析 Text 列單元格,而是遍歷 Trigger 列的字典鍵以在 Text 列單元格值中查找匹配項。
dict.CompareMode
設置為vbTextCompare
以進行不區分大小寫的比較以查找匹配項。 對於區分大小寫的比較,我們可以將其設置為vbBinaryCompare
Option Explicit
Public Function PartialStrMatch(str As String, matchCol As Range, lookupCol As Range) As String
Dim dict As Object, i As Long, cl As Range
Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
For Each cl In matchCol
If Not dict.exists(cl.Value) Then
dict.Add cl.Value, lookupCol(Application.Match(cl, matchCol, 0)).Value
End If
Next cl
For i = 0 To dict.Count - 1
If InStr(1, str, dict.Keys()(i), vbTextCompare) > 0 Then
PartialStrMatch = dict.Items()(i)
Exit For
End If
Next i
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.