简体   繁体   English

字符串比较VBA

[英]String comparison VBA

I am trying to complete a string comparison between a range and the cells to the left of it. 我试图完成一个范围和它左边的单元格之间的字符串比较。 So it goes through a range, looks at a cell, takes the value to the right and compares them. 因此它经过一个范围,查看一个单元格,将值右移并进行比较。 The end goal being to populate an array with all the values that match. 最终目标是使用所有匹配的值填充数组。

The issue I am having is when I use debug.print, the string's do seem to match. 我遇到的问题是当我使用debug.print时,字符串的确匹配。 However the StrComp evaluation i am using is evaluating to false. 但是我使用的StrComp评估结果为false。

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

Thanks in advance! 提前致谢!

This seems to me that you are comparing a item as String with a prod as Variant , because you did a confusion in your variables declaration. 在我看来,您正在将一个itemString与一个prodVariant ,因为您在变量声明中确实感到困惑。

Would you add 你会补充吗

Option Explicit

at the top, would you see that your code requires 在顶部,您是否看到您的代码需要

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

... This is Excel-VBA, not VB. ...这是Excel-VBA,而不是VB。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM