VBA Dictionary 中 CompareMode

請大師提供 comparemode 指令用法,最好有例子.......

1 Answer

Rating
  • 8 years ago
    Favorite Answer

    Set dic = CreateObject("Scripting.Dictionary")

    1. dic.CompareMode = vbTextCompare :設置字典的比較模式是本文,在這種比較模式下不區分大小寫,即 a 和 A 視為相同。vbTextCompare 的值為1,故上式亦可用 dic.CompareMode = 1 。

    2. dic.CompareMode = vbBinaryCompare,則設定為二進制比較,即區分大小寫,此時 a 和 A 視為相異。vbBinaryCompare 的值為0,故上式亦可用 dic.CompareMode = 0 。

    ======

    例:

    Set dic = CreateObject("Scripting.Dictionary")

    dic.CompareMode = vbTextCompare

    dic.Add "a", "apple"

    dic.Add "b", "banana"

    dic.Add "c", "cat"

    dic.Add "B", "book" '這裡將產生錯誤

    由於已設為本文模式比較,不區分大小寫,

    dic.Add "B", "book" 將產生添加失敗的錯誤,

    因為先前 dic.Add "b", "banana" 已在字典中加入關鍵字「b」(「B」和「b」視為一樣),

    而字典的關鍵字必須是唯一的,不能添加重複的關鍵字。

Still have questions? Get your answers by asking now.