【发布时间】:2017-01-18 13:18:20
【问题描述】:
我正在尝试完成一个范围与其左侧单元格之间的字符串比较。所以它通过一个范围,查看一个单元格,将值向右取并比较它们。最终目标是用所有匹配的值填充一个数组。
我遇到的问题是,当我使用 debug.print 时,字符串似乎确实匹配。然而,我正在使用的 StrComp 评估结果为假。
Sub FilterProdType()
'Get the product filter chosen and set to a variable
Dim bArray As Variant
Dim search, cell As Range
Dim prod, item As String
Set search = Sheet2.Range("B2:B41")
For Each cell In search
prod = Sheet1.Range("PRODTYPE").Value
item = cell.Offset(0, 1).Value
Debug.Print ("Value in range 'B2:B41':" & prod)
Debug.Print ("Value in cell directly left one column:" & item)
MsgBox StrComp(item, prod) = 0
Next cell
End Sub
提前致谢!
【问题讨论】:
-
试试
If item Like prod Then -
您在
item, prod之后尝试过, CompareMethod.Text或Binary吗?可能您只需要设置比较方法。还要检查是否有任何额外的空格,因为它们可能会导致它返回 false -
你试过旧的但黄金的物品 = prod 吗?
标签: excel string-comparison vba