VBA – 代码不会看到variables尽pipe是平等的

在我的一个macros中,我有一段代码(实际上是在不同的地方),它从string中删除任何特殊符号,然后比较数组中的值并查找重复项。 它看起来像这样:

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 – 它表示值实际上是相等的。 这两个variables都声明为string。 任何人都知道这里可能是错的吗?

编辑:我试图比较完整的string – 都给我Len = 854,我看不出任何与裸眼的差异。 我修剪和清理它们,使用StrComp,但代码告诉我,他们是不平等的。 在这里你可以看到两个string:

 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中的默认文本比较处于二进制模式。 这通常不是我们想要的,不pipe这是否会导致您的特定问题。

使用StrComp(str1, str2, vbTextCompare)来更好地比较string的方法。 如果这给出0 ,那么两个string是相等的。

一些更多细节在这里 。

首先检查一下string是否相等,如果不能完全避免它们的比较,也许是有用的。 请注意,虽然VBAstring可以包含大约20亿个Unicode字符,但是存在一些相关的其他限制(例如,公式不能超过255个字符,另请参见此处 )。

我将这个答案留在这里供将来参考,因为它可能对有类似问题的人有所帮助,尽pipe看起来并不能解决OP的问题。

在IF语句之前放置以下两行代码,运行代码并在即时窗口中分析结果。

 Debug.Print "Tab1 equals Tab2 = "; Tab1 = Tab2 Debug.Print "m = "; m, "i = "; i, "i <> m = "; i <> m 

检查你的声明。 确保你的variables都不是变体。 两个选项卡都应该是string,而m和I应该是Long或Integer。 通过simple =来优先考虑strComp(Tab1,Tab2,vbTextCompare)。 数字的等价物不是使用Doubletypes的数字,因为它们有零的困难。