返回列表 上一主題 發帖

[發問] excel VBA 從大量資料中裡找出共同重複的資料

本帖最後由 yen956 於 2015-12-31 20:12 編輯

回復 4# Hsieh
超版你好!!
請問: If Application.CountIf(Rng, a) > 1 Then d(a.Value) = ""
中的 d(a.Value) = "" 是什麼意思?
猜想是將 a 的 Key 丟到字典中, 或是將 a 的 Item 丟到字典中,
但為什麼是 d(a.Value) = "" 呢? 作用是什麼?
謝謝!!

TOP

感謝c_c_lai, GBKEE, 准提部林 等大大的說明,
因為一般文件只提到 d.Add "a", "Athens" 的方法,
現在總算知道 d(a.Value) = "" 這個方法的真正函義,
d(a.Value) = "" __key 為 a.value , item 為 空字符
謝謝大大們詳細的說明, 謝謝!!

TOP

花了不少時間, 總算對 Ditionary 有一點點了解,
d(a.Value) = ""  與
If Not exists(a.Value) Then d.Add a.Value, "" 同義.
下列修改自 15# GBKEE版大的VBA(版大的執行結果完全正確),
請指教!!
  1. '修改自 15# GBKEE版大的VBA
  2. Sub Test()
  3.     Dim d As Object, d2 As Object, AR(1 To 3), ArC()
  4.     Dim M As Variant, E As Variant
  5.     Dim LstA As Integer, LstB As Integer, Cnt As Integer, Cnt2 As Integer
  6.     Dim i As Integer, J As Integer
  7.     Set d = CreateObject("SCRIPTING.DICTIONARY")
  8.     Set d2 = CreateObject("SCRIPTING.DICTIONARY")
  9.     Range("C:G").ClearContents    '清除工作表4 C欄-->G欄
  10.     Range("E:E").Interior.ColorIndex = xlNone
  11.     LstB = [B65536].End(xlUp).Row
  12.     Cnt = 1
  13.     Cnt2 = 1
  14.     ArC = Array(35, 36, 37, 38)
  15.    
  16.     '以下計算 工作表1-> 工作表3 出現機率為80%的字母*****
  17.     For i = 1 To 3   '從 "工作表1" 到 "工作表3"
  18.         Sheets(i).Range("C:E").ClearContents    '清除每一頁的 C欄-->E欄
  19.         With Sheets(i).[C1].Resize(Sheets(i).[B65536].End(xlUp).Row)   '即[C1:Cxx]
  20.             .Cells = "=COUNTIF(C2,RC[-1])/COUNTA(C1)"
  21.             '[C1]公式 = COUNTIF(B:B,B1)/COUNTA(A:A), 即計算每個字母出現的機率
  22.             
  23.             AR(i) = Application.Transpose(.Offset(, -1).Resize(, 2).Value)
  24.             '將 B欄,C欄 轉置為 AR(1 to 2, 1 to 21)
  25.             
  26.             '將英文字母出現百分比機率導入 AR(i)
  27.             For Each E In .Cells    '歷遍每一頁的 [C1:Cxx]
  28.                 d2.Item(E.Offset(, -1).Value) = E.Value
  29.                 '將字母(Key)及機率(Item)全部存入 字典d2 中(不論機率大小)
  30.                
  31.                 If E >= 0.8 Then d(E.Offset(, -1).Value) = E.Value
  32.                 '將機率 >=80% 的字母及機率, 放到 字典d 中
  33.             Next
  34.             
  35.             .Cells = .Value             '公式轉為值
  36.             
  37.             If d.Count >= 1 Then
  38.                 .Cells(1).Range("B1").Resize(d.Count) = Application.Transpose(d.keys)   '傾倒字母到每頁的 [D1:Dxx]
  39.                 .Cells(1).Range("C1").Resize(d.Count) = Application.Transpose(d.Items)  '傾倒機率到每頁的 [E1:Exx]
  40.                 '★這裡若將 .Range("B1") 改為 .[B1]
  41.                 '  則會出現 "物件不支援屬性或方法" 的錯誤!!
  42.             End If
  43.             
  44.             If d2.Count >= 1 Then
  45.                 '固定式(工作表4的A欄及B欄的表格事先填好)
  46.                 '***工作表4上 B欄位中,每個英文字母出現在指定工作表的比率***
  47.                 M = Application.Match(Sheets(i).Name, [A:A])
  48.                 If IsNumeric(M) Then
  49.                     If i = 3 Then
  50.                         LstA = LstB
  51.                     Else
  52.                         LstA = Cells(M, 1).End(xlDown).Row - 1
  53.                     End If
  54.                     For J = M To LstA
  55.                         If d2.Exists(Cells(J, 2).Value) Then
  56.                             Cells(J, 3) = d2(Cells(J, 2).Value)
  57.                         End If
  58.                     Next
  59.                     Cnt = LstA + 1
  60.                 End If
  61.             
  62.                 '機動式(E欄(工作表名稱)及F欄(字母)的表格由VBA填入)
  63.                 '***工作表4上 F欄位中,每個英文字母出現在指定工作表的比率***
  64.                 Cells(Cnt2, 5) = Sheets(i).Name    '工作表名稱
  65.                 Cells(1).Range("E" & Cnt2).Resize(d2.Count).Interior.ColorIndex = ArC(i)
  66.                 Cells(1).Range("F" & Cnt2).Resize(d2.Count) = Application.Transpose(d2.keys)   '傾倒字母
  67.                 Cells(1).Range("G" & Cnt2).Resize(d2.Count) = Application.Transpose(d2.Items)  '傾倒機率
  68.                 Cnt2 = d2.Count + 1
  69.             End If
  70.         End With
  71.         d.RemoveAll
  72.         d2.RemoveAll
  73.         Sheets(i).Range("C:C", "E:E").NumberFormatLocal = "0%"  '數字格式化
  74.     Next
  75.     Range("C:C", "G:G").NumberFormatLocal = "0%"  '數字格式化
  76. End Sub
複製代碼

TOP

        靜思自在 : 人要知福、惜福、再造福。
返回列表 上一主題