vba - 如果函数将两个相同的文本进行比较,不同的值
问题描述
我的两个变量 PackageOptions 和 cell.Value 具有相同的文本但不同的值。是否有一种方法可以格式化它们以匹配以便代码跳转到 if 语句?我尝试使用 Trim$ 和 ascii 格式,但不明白如何格式化。
字符串一是 PackageOptions 类型 字符串二是 PackageOptions 类型
变量是从列表框中提取的:
Dim PackageOptions
Dim Customer As String
Dim PreferredDate As String
Dim ServicesLine1 As String
Dim PackageOptions As String
Dim GoogleR As Byte
Dim InvoiceR As Byte
For i = 0 To LbCustomer.ListCount - 1
If LbCustomer.Selected(i) Then
Customer = Customer & LbCustomer.List(i) & vbNewLine
PreferredDate = PreferredDate & LbCustomer.List(i, 1) & vbNewLine
ServicesLine1 = ServicesLine1 & LbCustomer.List(i, 2) & vbNewLine
**PackageOptions** = PackageOptions & LbCustomer.List(i, 3) & vbNewLine
GoogleR = GoogleR & LbCustomer.List(i, 4) & vbNewLine '
InvoiceR = InvoiceR & LbCustomer.List(i, 5) & vbNewLine
End If
Next i
Dim PrRange As Range
Dim cell As RangeSet PrRange = shPrice.Range("PricingTable[Item]")
Dim ItemRange As Range
Set ItemRange = shPrice.Range("PricingTable[Item Number]")
>looping through a table column, return the row and item number of match
>the variables have different values (shown in images attached)
For Each cell In PrRange
If PackageOptions = cell.Value Then
ItemR = cell.Row - shPrice.Range("PricingTable[[#Headers],[Item]]").Row
ItemNumber = shPrice.Range("PricingTable[Item Number]").Cells(ItemR, 1).Value
End If
Next cell
解决方案
我不太明白你的意思,特别是因为你没有说你在代码的哪一行遇到了哪个错误。也许您的抱怨只是比较没有给出您期望的结果。无论如何,您的变量PackageOptions
是 Variant 数据类型变量,其中包含一个字符串,该字符串正是Cell.Value
应该包含的,尽管您的支持屏幕将其显示为字符串。
PackageOptions = cell.Value
是一个非常粗略但坚固的比较。确保两个项目的数据类型相同。用于Cstr(PackageOptions)
将变体强制转换为字符串。也许在两个值上都使用它,Cstr(PackageOptions) = Cstr(cell.Value)
.
StrComp() 函数允许您有更多的调整空间,但正如其名称所承诺的那样,它比较两个字符串。因此结果不会有任何不同。下面的函数将比较不区分大小写的文本。如果两个字符串相等,它将返回 0(零)。
StrComp(Cstr(PackageOptions), Cstr(cell.Value), VbTextCompare)
推荐阅读
- recursion - 使用返回布尔值的递归函数时的指导?
- python-3.x - 如何在 python 中手动构建这个特定的字符串
- python - 了解 Group By 缺失值的行为并找出 .mean() 和手动求解均值之间的差异
- javascript - 如何在 Sequelize 中建立多对多关系?
- select - 敏捷工具包 atk4 选择多个或列表框字段
- git - git可以以独立于分支的方式跟踪文件吗?
- reactjs - React 本机,系统字体大小
- ruby-on-rails - Rails API - 将字符串参数转换为枚举值
- bluetooth - BLE 设备是否应该配对?或者设备必须配对的条件是什么?
- android - 如果另一个 TouchableOpacity 在其下方,则绝对放置的 TouchableOpacity 不可点击