如何修改此脚本以适应不同的情况?
How to modify this script to account for different case?
我写了一个 VBA 脚本,它在一列中搜索字符串,在另一列中 returns 匹配。重点是通过 SKU 将商品编号与其产品图片相匹配。问题是如果项目编号的大小写和图像的大小写不同,它会失败。脚本如下:
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 = Cells(I, "A").Value
v2 = ""
For J = 2 To NC
If InStr(Cells(J, "C").Value, v) > 0 Then
v2 = v2 & ";" & Cells(J, "C").Value
End If
Next J
Cells(I, "A").Offset(0, 1).Value = Mid(v2,2)
Next I
End Sub
如何解释不同的情况?有方法吗?
很简单,只需将其转换为小写即可:
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
v2 = v2 & ";" & Cells(J, "C").Value
End If
Next J
Cells(I, "A").Offset(0, 1).Value = Mid(v2,2)
Next I
End Sub
我刚刚获取了您的代码示例并将 v
和 Cells(J, "C").Value
.
转换为小写
我没有测试这个,所以我不能保证完美的功能。
您还可以使用
更改您的模块(在顶部)
Option Compare Text
这将完全关闭区分大小写(因此您不需要 LCase(..)
)
只需使用 Instr
function 的 vbTextCompare
选项即可。
If InStr(1, Cells(J, "C").Value, v, vbTextCompare) > 0 Then
v2 = v2 & ";" & Cells(J, "C").Value
End If
[vbTextCompare] Performs a text comparison, based on a case-insensitive text sort order determined by your application's current culture information.
我写了一个 VBA 脚本,它在一列中搜索字符串,在另一列中 returns 匹配。重点是通过 SKU 将商品编号与其产品图片相匹配。问题是如果项目编号的大小写和图像的大小写不同,它会失败。脚本如下:
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 = Cells(I, "A").Value
v2 = ""
For J = 2 To NC
If InStr(Cells(J, "C").Value, v) > 0 Then
v2 = v2 & ";" & Cells(J, "C").Value
End If
Next J
Cells(I, "A").Offset(0, 1).Value = Mid(v2,2)
Next I
End Sub
如何解释不同的情况?有方法吗?
很简单,只需将其转换为小写即可:
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
v2 = v2 & ";" & Cells(J, "C").Value
End If
Next J
Cells(I, "A").Offset(0, 1).Value = Mid(v2,2)
Next I
End Sub
我刚刚获取了您的代码示例并将 v
和 Cells(J, "C").Value
.
我没有测试这个,所以我不能保证完美的功能。
您还可以使用
更改您的模块(在顶部)Option Compare Text
这将完全关闭区分大小写(因此您不需要 LCase(..)
)
只需使用 Instr
function 的 vbTextCompare
选项即可。
If InStr(1, Cells(J, "C").Value, v, vbTextCompare) > 0 Then
v2 = v2 & ";" & Cells(J, "C").Value
End If
[vbTextCompare] Performs a text comparison, based on a case-insensitive text sort order determined by your application's current culture information.