【发布时间】:2017-04-11 09:16:17
【问题描述】:
在我的一个宏中,我有一段代码(实际上有几个,在不同的地方),它从字符串中删除任何特殊符号,然后比较数组中的值并查找重复项。它看起来像这样:
For m = LBound(tablica) To UBound(tablica)
For i = LBound(tablica) To UBound(tablica)
tab1 = Replace(Replace(Replace(Replace(LCase(tablica(i)), " ", ""), "™", ""), "®", ""), "©", "")
tab1 = Application.WorksheetFunction.Clean(tab1)
tab2 = Replace(Replace(Replace(Replace(LCase(tablica(m)), " ", ""), "™", ""), "®", ""), "©", "")
tab2 = Application.WorksheetFunction.Clean(tab2)
If tab1 = tab2 And i <> m Then
MsgBox "Duplicated note"
CheckDuplicates = True
Exit Function
End If
Next
Next
但是,代码看不到相等的值,即使它们是相等的。示例如下:
tab1 : "foruseonlyinareaswhereepafinaltier4/eustageivisrequired.turbocharged,chargeaircooledwetsleevecylinderlinersprogrammableauto-idleandauto-shutdownselectedidleadjustmentfrom900-1250rpmstart"
tab2 : "foruseonlyinareaswhereepafinaltier4/eustageivisrequired.turbocharged,chargeaircooledwetsleevecylinderlinersprogrammableauto-idleandauto-shutdownselectedidleadjustmentfrom900-1250rpmstart"
VBA 代码认为这两个值不相等。我试图将它们复制到工作表并在它们上面放一个简单的 IF - 它说这些值实际上是相等的。两个变量都被声明为字符串。有人知道这里可能出了什么问题吗?
编辑: 我试图比较完整的字符串 - 两者都给我 Len = 854,我肉眼看不出任何区别。我修剪并清理了它们,使用了 StrComp,但代码仍然告诉我它们不相等。在这里你可以看到两个字符串:
For use only in areas where EPA Final Tier 4/EU Stage IV is required. Turbocharged, Charge Air Cooled Wet Sleeve Cylinder Liners Programmable Auto-Idle and Auto-Shutdown Selected Idle Adjustment from 900-1250 RPM Starter Protection 4 Valves / Cylinder Cooled Exhaust Gas Recirculation Automatic Derating for Exceeded System Temperatures Electronically Controlled HPCR Fuel Delivery System, B20 Biodiesel Compatible Electrical Fuel Priming System Serpentine Drive Belt with Automatic Tensioner Under Hood Dual Element Air Cleaner with Restriction Indicator Under Hood Exhaust Filter and Catalysts with Curved Exhaust Stack Automatic Exhaust Filter Regeneration Dual-Stage Fuel Filter and Water Separator 500 Hour Vertical Spin-on Oil Filter Oil crankcase filter, Lifetime Engine Compartment Light Remote Jump Starting Lugs Automatic Engine Cool-down Timer
For use only in areas where EPA Final Tier 4/EU Stage IV is required. Turbocharged, Charge Air Cooled Wet Sleeve Cylinder Liners Programmable Auto-Idle and Auto-Shutdown Selected Idle Adjustment from 900-1250 RPM Starter Protection 4 Valves / Cylinder Cooled Exhaust Gas Recirculation Automatic Derating for Exceeded System Temperatures Electronically Controlled MEUI Fuel Delivery System, B20 Biodiesel Compatible Electrical Fuel Priming System Serpentine Drive Belt with Automatic Tensioner Under Hood Dual Element Air Cleaner with Restriction Indicator Under Hood Exhaust Filter and Catalysts with Curved Exhaust Stack Automatic Exhaust Filter Regeneration Dual-Stage Fuel Filter and Water Separator 500 Hour Vertical Spin-on Oil Filter Oil crankcase filter, Lifetime Engine Compartment Light Remote Jump Starting Lugs Automatic Engine Cool-down Timer
【问题讨论】:
-
您的 vba 是否说它们不相等,即使您将其直接分配给 2 个不同的字符串?你确定 i m 吗?
-
你检查过
i<>m(没有冒犯,但这是我花了几个小时盯着两个字符串时会想念的那种事情)。 -
@exSnake 是的,就是这样。 SJR 当然:P 在表中这些值位于第 4 和第 5 位。
-
如果我将这两个字符串放入一个数组并运行代码,它会显示一个
MsgBox,表示它们是重复的。 -
是的,谢谢。我想让我感到困惑的是本地变量中每个变量的值,因为我不知道它没有显示完整的字符串并且它们在那里似乎相等。