【问题标题】:String comparison VBA字符串比较 VBA
【发布时间】: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.TextBinary 吗?可能您只需要设置比较方法。还要检查是否有任何额外的空格,因为它们可能会导致它返回 false
  • 你试过旧的但黄金的物品 = prod 吗?

标签: excel string-comparison vba


【解决方案1】:

在我看来,您将itemStringprodVariant 进行比较,因为您在变量声明中混淆了。

你会添加

Option Explicit

在顶部,你会看到你的代码需要

Dim search As Range, cell As Range
Dim prod As String, item As String

...这是 Excel-VBA,不是 VB。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多