修改简单的VBA脚本来说明不同的匹配?

所以我使用这个function:

Sub Test() Dim NA As Long, NC As Long, v As String, I As Long, J As Long Dim v2 As String NA = Cells(Rows.Count, "A").End(xlUp).Row NC = Cells(Rows.Count, "C").End(xlUp).Row For I = 2 To NA v = LCase(Cells(I, "A").Value) v2 = "" For J = 2 To NC If InStr(LCase(Cells(J, "C").Value), v) > 0 Then ' What to do here? v2 = v2 & ";" & Cells(J, "C").Value End If Next J Cells(I, "A").Offset(0, 1).Value = Mid(v2,2) Next I End Sub 

将一列图像名称与其他列中的项目进行匹配。 除了一种情况,这很好:

如果项目编号在项目名称中包含“-SET2”string – 所以MCR7009A-SET2而不是MCR7009A – 文件名中没有SET2的图像不会以匹配结束。 这导致许多图像被忽略。

我怎样才能说明项目名称中存在“-SET2”的可能性?

在v的末尾检查-set2,如果存在的话,将其去掉

 Sub Test() Dim NA As Long, NC As Long, v As String, I As Long, J As Long Dim v2 As String NA = Cells(Rows.count, "A").End(xlUp).Row NC = Cells(Rows.count, "C").End(xlUp).Row For I = 2 To NA v = LCase(Cells(I, "A").Value) 'Check for -set2 on the end of v, if it is there, strip it out If Right(v, 5) = "-set2" Then v = Left(v, Len(v) - 5) End If v2 = "" For J = 2 To NC If InStr(LCase(Cells(J, "C").Value), v) > 0 Then ' What to do here? v2 = v2 & ";" & Cells(J, "C").Value End If Next J Cells(I, "A").Offset(0, 1).Value = Mid(v2, 2) Next I End Sub