[英]EXCEL VBA: match part of a string
我正在使用Excel VBA宏,該宏可以將V列中的相同條目與Y列中的條目匹配。一切正常,但有些條目僅與其他條目的一部分匹配。 例:
Column V: Column Y:
Word Excel
Word, Excel
Excel
現在對於此示例,它只會標記最后一個條目,但也應該標記第二個條目。 這是我現在實際擁有的代碼:
Sub MatchAndColor()
Dim lastRow As Long
Dim sheetName As String
sheetName = "MAIN" 'Name of the Sheet
lastRow = Sheets(sheetName).Range("V" & Rows.Count).End(xlUp).Row
lastRowB = Sheets(sheetName).Range("Y" & Rows.Count).End(xlUp).Row
For lrow = 2 To lastRow 'Loop through all rows
For lrowb = 2 To lastRowB 'Loop through all rows
If Sheets(sheetName).Cells(lrow, "V") Like Sheets(sheetName).Cells(lrowb, "Y") Then
Sheets(sheetName).Cells(lrow, "V").Interior.ColorIndex = 37 'Set Color to Light Blue
End If
Next lrowb
Next lrow
End Sub
謝謝您的幫助!
更新了答案以匹配問題更新,適用於給定的示例。
Sub MatchAndColor()
Dim lastRow As Long
Dim sheetName As String
sheetName = "MAIN" 'Name of the Sheet
lastRow = Sheets(sheetName).Range("V" & Rows.Count).End(xlUp).Row
lastRowB = Sheets(sheetName).Range("Y" & Rows.Count).End(xlUp).Row
For lrow = 2 To lastRow 'Loop through all rows
For lrowb = 2 To lastRowB 'Loop through all rows
If (Sheets(sheetName).Cells(lrow, "V") Like "*" & Sheets(sheetName).Cells(lrowb, "Y") & "*") Or (Sheets(sheetName).Cells(lrow, "Y") Like "*" & Sheets(sheetName).Cells(lrowb, "V") & "*") Then
Sheets(sheetName).Cells(lrow, "V").Interior.ColorIndex = 37 'Set Color to Light Blue
End If
Next lrowb
Next lrow
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.