在我的一个宏中,我有一段代码(实际上有几个,在不同的地方)从字符串中删除任何特殊符号,然后比较数组中的值并查找重复项。它看起来像这样:
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 - 它表示值ARE实际上是相等的。这两个变量都声明为字符串。任何人都知道这里可能出了什么问题?
编辑: 我试着比较完整的字符串 - 两个都给我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
答案 0 :(得分:1)
VBA中的默认文本比较是二进制模式。这通常不是我们想要的,无论这是否会导致您的特定问题。
安全比较字符串的更好方法是使用StrComp(str1, str2, vbTextCompare)
。如果这给出0
,则两个字符串相等。
更多细节here。
首先检查字符串是否相等以及它们是否完全避免它们的比较可能是有用的。请注意,虽然VBA字符串可以包含大约20亿个Unicode字符,但是存在一些相关的其他限制(例如,公式不能超过255个字符,另请参阅here)。
我将这个答案留在这里供将来参考,因为它可能对有类似问题的人有所帮助,虽然它似乎无法解决OP的问题。
答案 1 :(得分:0)
在IF语句之前放置以下两行代码,运行代码并在立即窗口中分析结果。
Debug.Print "Tab1 equals Tab2 = "; Tab1 = Tab2
Debug.Print "m = "; m, "i = "; i, "i <> m = "; i <> m
检查您的声明。确保您的变量都不是Variants。两个Tab都应该是字符串,m和I都应该是Longs或Integer。在简单的=
上优先考虑strComp(Tab1,Tab2,vbTextCompare)。数字的等价物不是使用Double类型数字,因为它们很难为零。