【问题标题】:Excel VBA: "invalid procedure call or method" when setting CompareMode to TextCompare with DictionaryExcel VBA:将CompareMode设置为TextCompare with Dictionary时出现“无效的过程调用或方法”
【发布时间】:2014-08-14 07:10:26
【问题描述】:

由于某种原因,我无法将字典 offONameDic 中的 CompareMode 设置为 TextCompare。我不断收到"Invalid Procedure or Call Error"。我想确保用户提供的密钥的大写不会影响字典将密钥识别为有效的能力,即如果密钥是“hello”,“Hello”也将被识别为密钥。

请注意,我使用 VBA Excel 中已安装库列表中的 Microsoft 脚本运行时参考

我的代码:

Sub Main()
'...code...
Dim offONameDic As Scripting.Dictionary
'...code...
Set offONameDic = New Scripting.Dictionary
With offONameDic
    .Add "Blood", "Blood"
    .Add "Liver", "Liver"
    .Add "Kidneys", "Kidneys"
    .Add "Kidney", "Kidneys"
    .Add "Spleen", "Spleen"
    .Add "Heart", "Heart"
    .Add "Lungs", "Lungs"
    .Add "Lung", "Lungs"
    .Add "Stomach", "Stomach"
    .Add "Muscle", "Muscle"
    .Add "Bone", "Bone"
    .Add "Carcass", "Carcass"
    .Add "Tumor", "Tumor"
    .Add "Axial Lymph Node", "Axial Lymph Node"
    .Add "ALN", "Axial Lymph Node"
    .Add "Igunial Lymph Node", "Igunial Lymph Node"
    .Add "IgLN", "Igunial Lymph Node"
    .Add "Intestines", "Intestines"
    .Add "Intestine", "Intestines"
    .Add "Cecum", "Cecum"
    .Add "Tail", "Tail"
    .CompareMode = TextCompare '****This Line Causes the "Invalid Procedure or Call" Error"****
End With
'...code...
End Sub

【问题讨论】:

    标签: excel vba dictionary string-comparison text-comparison


    【解决方案1】:

    您需要设置CompareMode 属性在向字典添加任何数据之前,因此将导致错误的行移到第一个之后With offONameDic 行,例如:

    With offONameDic
        .CompareMode = TextCompare ' No longer gives an error!
        .Add "Blood", "Blood"
        ...
    

    根据the documentation

    如果您尝试更改已包含数据的 Dictionary 对象的比较模式,则会发生错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多