[英]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.