【问题标题】:VBA - code does not see variables as equal even though they areVBA - 代码不会将变量视为相等,即使它们是
【发布时间】: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&lt;&gt;m(没有冒犯,但这是我花了几个小时盯着两个字符串时会想念的那种事情)。
  • @exSnake 是的,就是这样。 SJR 当然:P 在表中这些值位于第 4 和第 5 位。
  • 如果我将这两个字符串放入一个数组并运行代码,它会显示一个MsgBox,表示它们是重复的。
  • 是的,谢谢。我想让我感到困惑的是本地变量中每个变量的值,因为我不知道它没有显示完整的字符串并且它们在那里似乎相等。

标签: vba excel


【解决方案1】:

VBA 中的默认文本比较是二进制模式。这通常不是我们想要的,无论这是否会导致您的特定问题。

安全比较字符串的更好方法是使用StrComp(str1, str2, vbTextCompare)。如果这给出了0,那么这两个字符串是相等的。

更多细节here

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

我将这个答案保留在这里以供将来参考,因为它可能对遇到类似问题的人有所帮助,尽管它似乎不能解决 OP 的问题。

【讨论】:

  • @kshaq - 您粘贴到问题中的两个字符串在使用StrComp(tablica(1), tablica(2), vbTextCompare) 时为我提供了0。听起来好像您拥有的字符串与我们复制/粘贴它们时得到的字符串不一样。
  • @kshaq 尝试在即时窗口中执行?Len("one of the strings"),看看它是否显示 186 - 这就是我们看到的字符数
  • 顺便说一句,我不认为投反对票是“苛刻”或个人的,它只是游戏的一部分,应该采取一些公平竞争的方式。它涉及答案的相关性,而不是答案的发布者。我也相信好的实践建议更适合 cmets 部分。
  • @A.S.H 我可以看到你来自哪里,这确实是一个公平的观点。在这种情况下我回答得太快了,事后看来我会发表评论。就个人而言,我保留对质量差的答案的反对票,例如那些在特定示例案例之外失败的答案,但当然这因用户而异(当然没有像你提到的那样使其个人化)
  • “请注意,VBA 字符串的最大长度为 255 个字符” - 您链接到的示例不是指字符串的最大长度,而是指可以传递的字符串的最大长度在Application.OntimeProcedure 参数中。 String 变量本身不限于 255 个字符,例如OP 的字符串中有 854 个字符。
【解决方案2】:

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

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

检查您的声明。确保您的所有变量都不是变体。两个制表符都应该是字符串,m 和 I 都应该是 Longs 或 Integer。请优先考虑 strComp(Tab1, Tab2, vbTextCompare) 而不是简单的=。数字的等价物是不要使用 Double 类型的数字,因为它们难以为零。

【讨论】:

  • 声明没问题,不幸的是 StrComp 没有改变输出。
  • 我上面的测试代码的打印结果将确定两个比较中的哪一个与预期不同。如果事实证明,正如您所怀疑的那样,这是字符串比较,下一个问题将是这些字符串的长度:Debug.Print Len(Tab1), Len(Tab2)
猜你喜欢
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多