iT邦幫忙

1

Excel vba 條件排序怎麼寫?

  • 分享至 

  • xImage

請vba高手幫幫忙,
我有一個excel如圖片,
想寫vba來做排序,
條件是根據總排名來排序,
但還要比對本身自己的部門排名,
像例子里面AA雖然總排名第1,
但在部門3里面他是第2,
所以他就必須讓出來,
調整排名到CC(部門3第1名)後面,
BB則往前遞補取代第1,
最終結果就是像E欄調整後排名,
請問vba高手如何寫呢?
https://ithelp.ithome.com.tw/upload/images/20220228/20146964yWdVQx9bJV.jpg

看更多先前的討論...收起先前的討論...
echochio iT邦高手 1 級 ‧ 2022-02-28 12:31:05 檢舉
這與 VBA 無關
您先搞定 AND OR 的邏輯
櫻花 iT邦新手 5 級 ‧ 2022-02-28 15:36:35 檢舉
可以麻煩大大解釋一下怎麼做嗎?
意思是。你不可能兩個條件同時排序。會打架。
你只能唯一排序。或是先依OOO排再依XXX排。
兩個同時排序是不可能辦到的事。
你不可能要求「最高的排前面跟最低的排前面」
一定打架的。
我覺得他要的邏輯可能是各組內需要照名次重新排名總排名.. 先照部門代號(1st)/部門排名 (2nd)整筆排序, 再各別依部門代號對應的"調整後排名"的格子進行排序(只排序格子, 不排序整筆資料), 最後再重新依學生名排序回來..
F= 部門排名(Column C) * 1000 + 總排名(Column D)
之後以 F 欄排序
感覺很像您的需求
櫻花 iT邦新手 5 級 ‧ 2022-03-01 18:12:18 檢舉
感謝各位大大幫忙。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
rogeryao
iT邦超人 8 級 ‧ 2022-03-02 11:01:39
最佳解答
Private Sub CommandButton1_Click()
Dim rowmax As Integer
Dim temp1, temp2 As String
Dim temp3, temp4 As Integer
Dim getdata As Boolean
Dim num As Integer
Dim no As Integer

num = 0
no = 0
rowmax = Range("A1").End(xlDown).Row

'清除調整後排名
For i = 2 To rowmax
  Cells(i, 5) = ""
Next i

'原始資料先依照總排名、部門排名、部門代號、學生姓名排序
Range("A1:D" & rowmax).Select
ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Add Key:=Range("D2:D" & rowmax), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Add Key:=Range("C2:C" & rowmax), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Add Key:=Range("B2:B" & rowmax), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Add Key:=Range("A2:A" & rowmax), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("工作表1").Sort
    .SetRange Range("A1:D" & rowmax)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

For i = 2 To rowmax - 1
  getdata = False
  For x = i + 1 To rowmax
    If Cells(x, 2) = Cells(i, 2) Then
      '同部門裡,部門排名在前面的人優先排名或部門裡同名次,總排名在前面的人優先排名
      If (Cells(x, 3) < Cells(i, 3)) Or (Cells(x, 3) = Cells(i, 3) And Cells(x, 4) < Cells(i, 4)) Then      
        getdata = True
        num = i
        no = x
        Exit For
      End If
    End If
  Next x
  If getdata = True Then
    Rows(num).Cut
    Rows(no + 1).Insert
    i = num - 1
  End If
Next i

'調整後排名,並列排名(連號)
'Dim orderno As Integer
'For i = 2 To rowmax
'  If i = 2 Then
'    orderno = 1
'  Else
'    If (Cells(i, 3) <> Cells(i - 1, 3)) Or (Cells(i, 4) <> Cells(i - 1, 4)) Then
'      orderno = orderno + 1
'    End If
'  End If
'  Cells(i, 5) = orderno
'Next i

'調整後排名,並列排名(不連號)
For i = 2 To rowmax
  Cells(i, 5) = i - 1
  If i > 2 Then
    If (Cells(i, 3) = Cells(i - 1, 3)) And (Cells(i, 4) = Cells(i - 1, 4)) Then
      Cells(i, 5) = Cells(i - 1, 5)
    End If
  End If
Next i

MsgBox "ok"
End Sub

https://ithelp.ithome.com.tw/upload/images/20220302/20085021b1OqC6tBHf.png
https://ithelp.ithome.com.tw/upload/images/20220302/200850210ptuhxIeb5.png
https://ithelp.ithome.com.tw/upload/images/20220302/20085021rwR0tXt9fG.png

櫻花 iT邦新手 5 級 ‧ 2022-03-02 14:36:20 檢舉

非常感謝roger大的幫忙,太厲害了!

1
海綿寶寶
iT邦大神 1 級 ‧ 2022-03-01 17:45:58

寫出來了
1.原資料
https://ithelp.ithome.com.tw/upload/images/20220301/20001787bAKLf72Vum.png
2.執行VBA後
https://ithelp.ithome.com.tw/upload/images/20220301/20001787wFxlR8iTY6.png
3.手動加入新排名後
https://ithelp.ithome.com.tw/upload/images/20220301/20001787Kumkz9RGkA.png
4.最後結果(以原排序)
https://ithelp.ithome.com.tw/upload/images/20220301/20001787MGI7wyIm5S.png

櫻花 iT邦新手 5 級 ‧ 2022-03-01 18:06:17 檢舉

請問大大怎麼寫呢?可否提供程式碼?感謝!

我要發表回答

立即登入回答