- 帖子
- 97
- 主題
- 33
- 精華
- 0
- 積分
- 129
- 點名
- 0
- 作業系統
- Win 7
- 軟體版本
- office 2007
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2019-5-7
- 最後登錄
- 2022-8-25
|
8#
發表於 2019-8-15 08:38
| 只看該作者
這是我做出來的結果給大家參考~~~
- Sub 按出現次數排序()
- Application.ScreenUpdating = False
- Dim Arr, a, xD, Brr(1 To 20000, 1 To 2), N&
- ThisWorkbook.Sheets("量測").Range("H2:I" & Range("H" & Rows.Count).End(xlDown).Row).Clear
- Arr = Range("B2:F31")
- Set xD = CreateObject("Scripting.Dictionary")
- If ThisWorkbook.Sheets("量測").[B2] = "" Then Exit Sub
- 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
- '排序
- If N = 0 Then Exit Sub
- With ThisWorkbook.Sheets("量測").[H2:I2].Resize(N)
- .Value = Brr
- .Sort Key1:=.Item(2), Order1:=xlDescending, _
- Key2:=.Item(1), Order2:=xlDescending, Header:=xlNo
- End With
- '儲存格置中、格線、小數點
- With ThisWorkbook.Sheets("量測").Range("H1:I" & Range("H" & Rows.Count).End(xlUp).Row)
- .NumberFormatLocal = "0.00_ "
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .Borders(xlEdgeLeft).LineStyle = xlContinuous
- .Borders(xlEdgeTop).LineStyle = xlContinuous
- .Borders(xlEdgeBottom).LineStyle = xlContinuous
- .Borders(xlEdgeRight).LineStyle = xlContinuous
- .Borders(xlInsideVertical).LineStyle = xlContinuous
- .Borders(xlInsideHorizontal).LineStyle = xlContinuous
- End With
- With ThisWorkbook.Sheets("量測").Range("I1:I11")
- .NumberFormatLocal = "0"
- End With
- '找最大值、最小值
- With ThisWorkbook.Sheets("量測").Range("K1:L2")
- [k1].Value = "最大值": [L1].Value = "最小值"
- Range("K1:L1").Font.Bold = True
- Range("K1:L1").Name = "新細明體"
- .Font.Size = 12
- Range("K1:L1").Interior.Color = RGB(217, 226, 243)
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .Borders(xlEdgeLeft).LineStyle = xlContinuous
- .Borders(xlEdgeTop).LineStyle = xlContinuous
- .Borders(xlEdgeBottom).LineStyle = xlContinuous
- .Borders(xlEdgeRight).LineStyle = xlContinuous
- .Borders(xlInsideVertical).LineStyle = xlContinuous
- .Borders(xlInsideHorizontal).LineStyle = xlContinuous
- End With
- [k2] = Application.Max(Arr)
- [L2] = Application.Min(Arr)
- '找對應的值,並填滿顏色
- For x = 2 To 6
- For Y = 2 To 31
- Cells(Y, x).Interior.Color = xlNone
- If Cells(Y, x).Value = [H2].Value And [H2] <> "" Then
- Cells(Y, x).Interior.Color = RGB(252, 216, 162)
- [H2].Interior.Color = RGB(252, 216, 162)
- End If
- If Cells(Y, x).Value = [H3].Value And [H3] <> "" Then
- Cells(Y, x).Interior.Color = RGB(144, 248, 169)
- [H3].Interior.Color = RGB(144, 248, 169)
- End If
- If Cells(Y, x).Value = [H4].Value And [H4] <> "" Then
- Cells(Y, x).Interior.Color = RGB(170, 250, 252)
- [H4].Interior.Color = RGB(170, 250, 252)
- End If
- If Cells(Y, x).Value = [k2].Value Then
- Cells(Y, x).Font.Color = RGB(0, 0, 255)
- [k2].Font.Color = RGB(0, 0, 255)
- Cells(Y, x).Font.Bold = True
- End If
- If Cells(Y, x).Value = [L2].Value Then
- Cells(Y, x).Font.Color = RGB(255, 0, 0)
- [L2].Font.Color = RGB(255, 0, 0)
- Cells(Y, x).Font.Bold = True
- End If
- Next Y
- Next x
- '設定欄寬
- Columns("A:L").ColumnWidth = 8
- Columns("G").ColumnWidth = 3
- Columns("J").ColumnWidth = 3
- End Sub
- '=================================
- Sub 清除()
- ThisWorkbook.Sheets("量測").Range("B2:F31").ClearContents
- With ThisWorkbook.Sheets("量測").Range("B2:F31")
- .NumberFormatLocal = "0.00_ "
- .Borders(xlEdgeLeft).LineStyle = xlContinuous
- .Borders(xlEdgeTop).LineStyle = xlContinuous
- .Borders(xlEdgeBottom).LineStyle = xlContinuous
- .Borders(xlEdgeRight).LineStyle = xlContinuous
- .Borders(xlInsideVertical).LineStyle = xlContinuous
- .Borders(xlInsideHorizontal).LineStyle = xlContinuous
- .Interior.Color = xlNone
- .Font.Bold = False
- .Font.Color = RGB(0, 0, 0)
- End With
- Range("B2").Select
- End Sub
複製代碼 |
|