Board logo

標題: VBA 計算數據出現次數並排序 [打印本頁]

作者: s13030029    時間: 2019-8-9 16:32     標題: VBA 計算數據出現次數並排序

本帖最後由 s13030029 於 2019-8-9 16:34 編輯

Q1:請問要如何不顯示或計算空白的儲存格??
Q2:請問要如何不顯示次數小於1的儲存格??

[attach]31142[/attach]
  1. Sub 按出現次數排序()
  2.     Dim d As Object
  3.     Dim Arr
  4.     Dim i As Integer, j As Integer
  5.     Application.ScreenUpdating = False
  6.     Range("A15:B30").Clear
  7.     '提取不重複值併計算出現次數
  8.     Set d = CreateObject("Scripting.Dictionary")
  9.     Arr = Range("A2:H6")
  10.     For i = 1 To UBound(Arr, 1) '計算陣列大小(列)
  11.         For j = 1 To UBound(Arr, 2) '計算陣列大小(欄)
  12.             If Not d.Exists(Arr(i, j)) Then
  13.                 d.Add Arr(i, j), 1
  14.             Else
  15.                 d.Item(Arr(i, j)) = d.Item(Arr(i, j)) + 1
  16.             End If
  17.         Next
  18.     Next
  19.    
  20.     '輸出並排序
  21.     Range("A15").Resize(d.Count) = Application.Transpose(d.keys)
  22.     Range("B15").Resize(d.Count) = Application.Transpose(d.items)
  23.     Range("A15:B15").Resize(d.Count).Sort key1:=Range("B14"), Order1:=xlDescending
  24.    
  25.     Set d = Nothing
  26.     Application.ScreenUpdating = True
  27. End Sub
複製代碼
[attach]31141[/attach]
作者: 准提部林    時間: 2019-8-10 10:13

Sub 按出現次數排序()
Dim Arr, A, xD, Brr(1 To 20000, 1 To 2), N&
Range("A15:B30").Clear
Arr = Range("A2:H6")
Set xD = CreateObject("Scripting.Dictionary")
For Each A In Arr
    If A = "" Then GoTo 101
    xD(A) = xD(A) + 1
    If xD(A) = 2 Then N = N + 1:  xD(A & "S") = N:  Brr(N, 1) = A
    If xD(A) > 1 Then Brr(xD(A & "S"), 2) = xD(A)
101: Next
With [A15:B15].Resize(N)
     .Value = Brr
     .Sort Key1:=.Item(2), Order1:=xlDescending, _
           Key2:=.Item(1), Order2:=xlDescending, Header:=xlNo
End With
End Sub


==========================================
作者: s13030029    時間: 2019-8-12 13:32

回復 2# 准提部林
那如果說我的數據可能沒有全部輸入完,但是每筆出現的次數都只有一次
這段程式碼就會出現錯誤,這要怎麼改?
With [G1:H1].Resize(N)
     .Value = Brr
     .Sort Key1:=.Item(2), Order1:=xlDescending, _
           Key2:=.Item(1), Order2:=xlDescending, Header:=xlNo
End With
作者: 准提部林    時間: 2019-8-12 14:23

回復 3# s13030029

   IF N=0 THEN EXIT SUB '加這一行
   With [A15:B15].Resize(N)
     .Value = Brr
     .Sort Key1:=.Item(2), Order1:=xlDescending, _
           Key2:=.Item(1), Order2:=xlDescending, Header:=xlNo
   End With
作者: s13030029    時間: 2019-8-12 15:28

回復 4# 准提部林

原來如此~謝謝准大~~
作者: s13030029    時間: 2019-8-14 10:34

本帖最後由 s13030029 於 2019-8-14 10:44 編輯

回復 2# 准提部林
准大~
我想再請問一下,如果要再找出最大值最小值,並用.Interior.Color 標記成紅色的話,要怎麼改???
(出現次數等於1的部分可能還是要顯示出來)
[attach]31153[/attach]
  1. Sub 按出現次數排序()
  2.     Application.ScreenUpdating = False
  3.     Dim Arr, a, xD, Brr(1 To 20000, 1 To 2), N&
  4.     ThisWorkbook.Sheets("量測").Range("H2:I" & Range("H" & Rows.Count).End(xlDown).Row).Clear
  5.     Arr = Range("B2:F31")
  6.     Set xD = CreateObject("Scripting.Dictionary")
  7.     If ThisWorkbook.Sheets("量測").[B2] = "" Then Exit Sub
  8.     For Each a In Arr
  9.         If a = "" Then GoTo 101
  10.         xD(a) = xD(a) + 1
  11.         If xD(a) = 1 Then N = N + 1:  xD(a & "S") = N:  Brr(N, 1) = a
  12.         If xD(a) > 1 Then Brr(xD(a & "S"), 2) = xD(a)
  13. 101:     Next
  14.     If N = 0 Then Exit Sub
  15.     With ThisWorkbook.Sheets("量測").[H2:I2].Resize(N)
  16.          .Value = Brr
  17.          .Sort Key1:=.Item(2), Order1:=xlDescending, _
  18.                Key2:=.Item(1), Order2:=xlDescending, Header:=xlNo
  19.     End With

  20.     With ThisWorkbook.Sheets("量測").Range("H1:I" & Range("H" & Rows.Count).End(xlUp).Row)
  21.          .NumberFormatLocal = "0.00_ "
  22.         .HorizontalAlignment = xlCenter
  23.         .VerticalAlignment = xlCenter
  24.         .Borders(xlEdgeLeft).LineStyle = xlContinuous
  25.         .Borders(xlEdgeTop).LineStyle = xlContinuous
  26.         .Borders(xlEdgeBottom).LineStyle = xlContinuous
  27.         .Borders(xlEdgeRight).LineStyle = xlContinuous
  28.         .Borders(xlInsideVertical).LineStyle = xlContinuous
  29.         .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  30.     End With
  31.     With ThisWorkbook.Sheets("量測").Range("I1:I11")
  32.         .NumberFormatLocal = "0"
  33.     End With
  34.     For x = 2 To 6
  35.     For Y = 2 To 31
  36.         Cells(Y, x).Interior.Color = xlNone
  37.         If Cells(Y, x).Value = Cells(2, "H").Value And Cells(2, "H") <> "" Then
  38.             Cells(Y, x).Interior.Color = RGB(252, 216, 162)
  39.             Cells(2, "H").Interior.Color = RGB(252, 216, 162)
  40.         End If
  41.         If Cells(Y, x).Value = Cells(3, "H").Value And Cells(3, "H") <> "" Then
  42.             Cells(Y, x).Interior.Color = RGB(144, 248, 169)
  43.             Cells(3, "H").Interior.Color = RGB(144, 248, 169)
  44.         End If
  45.         If Cells(Y, x).Value = Cells(4, "H").Value And Cells(4, "H") <> "" Then
  46.             Cells(Y, x).Interior.Color = RGB(170, 250, 252)
  47.             Cells(4, "H").Interior.Color = RGB(170, 250, 252)
  48.         End If
  49.     Next Y
  50.     Next x
  51.     Application.ScreenUpdating = True
  52. End Sub
  53. '=================================
  54. Sub 清除()
  55.     ThisWorkbook.Sheets("量測").Range("B2:F31").ClearContents
  56.     With ThisWorkbook.Sheets("量測").Range("B2:F31")
  57.         .NumberFormatLocal = "0.00_ "
  58.         .Borders(xlEdgeLeft).LineStyle = xlContinuous
  59.         .Borders(xlEdgeTop).LineStyle = xlContinuous
  60.         .Borders(xlEdgeBottom).LineStyle = xlContinuous
  61.         .Borders(xlEdgeRight).LineStyle = xlContinuous
  62.         .Borders(xlInsideVertical).LineStyle = xlContinuous
  63.         .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  64.         .Interior.Color = xlNone
  65.     End With
  66.     Range("B2").Select
  67. End Sub
複製代碼

作者: hcm19522    時間: 2019-8-14 18:49

https://blog.xuite.net/hcm19522/twblog/588109997
作者: s13030029    時間: 2019-8-15 08:38

這是我做出來的結果給大家參考~~~
[attach]31154[/attach]
  1. Sub 按出現次數排序()
  2.     Application.ScreenUpdating = False
  3.     Dim Arr, a, xD, Brr(1 To 20000, 1 To 2), N&
  4.     ThisWorkbook.Sheets("量測").Range("H2:I" & Range("H" & Rows.Count).End(xlDown).Row).Clear
  5.     Arr = Range("B2:F31")
  6.     Set xD = CreateObject("Scripting.Dictionary")
  7.     If ThisWorkbook.Sheets("量測").[B2] = "" Then Exit Sub
  8.     For Each a In Arr
  9.         If a = "" Then GoTo 101
  10.         xD(a) = xD(a) + 1
  11.         If xD(a) = 2 Then N = N + 1:  xD(a & "S") = N:  Brr(N, 1) = a
  12.         If xD(a) > 1 Then Brr(xD(a & "S"), 2) = xD(a)
  13. 101:     Next
  14.     '排序
  15.     If N = 0 Then Exit Sub
  16.     With ThisWorkbook.Sheets("量測").[H2:I2].Resize(N)
  17.          .Value = Brr
  18.          .Sort Key1:=.Item(2), Order1:=xlDescending, _
  19.                Key2:=.Item(1), Order2:=xlDescending, Header:=xlNo
  20.     End With
  21.     '儲存格置中、格線、小數點
  22.     With ThisWorkbook.Sheets("量測").Range("H1:I" & Range("H" & Rows.Count).End(xlUp).Row)
  23.          .NumberFormatLocal = "0.00_ "
  24.         .HorizontalAlignment = xlCenter
  25.         .VerticalAlignment = xlCenter
  26.         .Borders(xlEdgeLeft).LineStyle = xlContinuous
  27.         .Borders(xlEdgeTop).LineStyle = xlContinuous
  28.         .Borders(xlEdgeBottom).LineStyle = xlContinuous
  29.         .Borders(xlEdgeRight).LineStyle = xlContinuous
  30.         .Borders(xlInsideVertical).LineStyle = xlContinuous
  31.         .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  32.     End With
  33.     With ThisWorkbook.Sheets("量測").Range("I1:I11")
  34.         .NumberFormatLocal = "0"
  35.     End With
  36.     '找最大值、最小值
  37.     With ThisWorkbook.Sheets("量測").Range("K1:L2")
  38.          [k1].Value = "最大值": [L1].Value = "最小值"
  39.          Range("K1:L1").Font.Bold = True
  40.          Range("K1:L1").Name = "新細明體"
  41.         .Font.Size = 12
  42.         Range("K1:L1").Interior.Color = RGB(217, 226, 243)
  43.         .HorizontalAlignment = xlCenter
  44.         .VerticalAlignment = xlCenter
  45.         .Borders(xlEdgeLeft).LineStyle = xlContinuous
  46.         .Borders(xlEdgeTop).LineStyle = xlContinuous
  47.         .Borders(xlEdgeBottom).LineStyle = xlContinuous
  48.         .Borders(xlEdgeRight).LineStyle = xlContinuous
  49.         .Borders(xlInsideVertical).LineStyle = xlContinuous
  50.         .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  51.     End With
  52.     [k2] = Application.Max(Arr)
  53.     [L2] = Application.Min(Arr)
  54.     '找對應的值,並填滿顏色
  55.     For x = 2 To 6
  56.     For Y = 2 To 31
  57.         Cells(Y, x).Interior.Color = xlNone
  58.         If Cells(Y, x).Value = [H2].Value And [H2] <> "" Then
  59.             Cells(Y, x).Interior.Color = RGB(252, 216, 162)
  60.             [H2].Interior.Color = RGB(252, 216, 162)
  61.         End If
  62.         If Cells(Y, x).Value = [H3].Value And [H3] <> "" Then
  63.             Cells(Y, x).Interior.Color = RGB(144, 248, 169)
  64.             [H3].Interior.Color = RGB(144, 248, 169)
  65.         End If
  66.         If Cells(Y, x).Value = [H4].Value And [H4] <> "" Then
  67.             Cells(Y, x).Interior.Color = RGB(170, 250, 252)
  68.             [H4].Interior.Color = RGB(170, 250, 252)
  69.         End If
  70.         If Cells(Y, x).Value = [k2].Value Then
  71.             Cells(Y, x).Font.Color = RGB(0, 0, 255)
  72.             [k2].Font.Color = RGB(0, 0, 255)
  73.             Cells(Y, x).Font.Bold = True
  74.         End If
  75.         If Cells(Y, x).Value = [L2].Value Then
  76.             Cells(Y, x).Font.Color = RGB(255, 0, 0)
  77.             [L2].Font.Color = RGB(255, 0, 0)
  78.             Cells(Y, x).Font.Bold = True
  79.         End If
  80.     Next Y
  81.     Next x
  82.     '設定欄寬
  83.     Columns("A:L").ColumnWidth = 8
  84.     Columns("G").ColumnWidth = 3
  85.     Columns("J").ColumnWidth = 3
  86. End Sub
  87. '=================================
  88. Sub 清除()
  89.     ThisWorkbook.Sheets("量測").Range("B2:F31").ClearContents
  90.     With ThisWorkbook.Sheets("量測").Range("B2:F31")
  91.         .NumberFormatLocal = "0.00_ "
  92.         .Borders(xlEdgeLeft).LineStyle = xlContinuous
  93.         .Borders(xlEdgeTop).LineStyle = xlContinuous
  94.         .Borders(xlEdgeBottom).LineStyle = xlContinuous
  95.         .Borders(xlEdgeRight).LineStyle = xlContinuous
  96.         .Borders(xlInsideVertical).LineStyle = xlContinuous
  97.         .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  98.         .Interior.Color = xlNone
  99.         .Font.Bold = False
  100.         .Font.Color = RGB(0, 0, 0)
  101.     End With
  102.     Range("B2").Select
  103. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)