简体   繁体   中英

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. However the StrComp evaluation i am using is evaluating to 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.

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.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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