簡體   English   中英

字符串比較VBA

[英]String comparison VBA

我試圖完成一個范圍和它左邊的單元格之間的字符串比較。 因此它經過一個范圍,查看一個單元格,將值右移並進行比較。 最終目標是使用所有匹配的值填充數組。

我遇到的問題是當我使用debug.print時,字符串的確匹配。 但是我使用的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

提前致謝!

在我看來,您正在將一個itemString與一個prodVariant ,因為您在變量聲明中確實感到困惑。

你會補充嗎

Option Explicit

在頂部,您是否看到您的代碼需要

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

...這是Excel-VBA,而不是VB。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM