2012-03-14 155 views
0

我想知道是否有人可以幫助我擴展下面的代碼來處理6列。它已經適用於任何數量的行。如何爲列添加相同的結構?用戶名:assylias構造了這段代碼,我試圖根據我的排序需求來調整它。VBA排序 - 擴展6列的代碼

問題: 我需要排序是這樣的

X A 3 
X B 7 
X C 2 
X D 4 
Y E 8 
Y A 9 
Y B 11 
Y F 2 

它需要如下進行排序:其中,X和Y列代表組。字母:A,B,C,D,E,F代表該組的成員。這些數字是我們正在比較的一些指標。獲得該編號的最高編號和相關成員是該編組的「領導者」,並且我想對數據進行排序,以便每個編組的每個領導者按照以下方式與該編組的每個成員進行比較:

X B A 3 
X B C 2 
X B D 4 
Y B E 8 
Y B A 9 
Y B F 2 

說明:B恰好是兩組的領導者。我需要將他與其他所有成員以及他們單元右側的數據進行比較,並列出他們獲得的數字。

問題:配備Assylias的代碼,我現在試圖擴展到我的數據集。我的數據集有6列,所以有很多定性列來描述每個成員(如狀態,ID#等),我需要幫助擴展代碼來涵蓋這一點。另外,如果可能的話,對某些步驟(可能以評論形式)的解釋將使我能夠更好地連接點。 (大多數情況下,我不明白是什麼dict1/dict2是和他們在做...(dict1.exists(數據(i,1)),例如不明擺着我。

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
doIt 
End Sub 
Public Sub doIt() 

Dim data As Variant 
Dim result As Variant 
Dim i As Long 
Dim j As Long 
Dim dict1 As Variant 
Dim dict2 As Variant 

Set dict1 = CreateObject("Scripting.Dictionary") 
Set dict2 = CreateObject("Scripting.Dictionary") 
data = Sheets("Sheet1").UsedRange 

For i = LBound(data, 1) To UBound(data, 1) 
    If dict1.exists(data(i, 1)) Then 
     If dict2(data(i, 1)) < data(i, 3) Then 
      dict1(data(i, 1)) = data(i, 2) 
      dict2(data(i, 1)) = data(i, 3) 
     End If 
    Else 
     dict1(data(i, 1)) = data(i, 2) 
     dict2(data(i, 1)) = data(i, 3) 
    End If 
Next i 

ReDim result(LBound(data, 1) To UBound(data, 1) - dict1.Count, 1 To 4) As Variant 

j = 1 
For i = LBound(data, 1) To UBound(data, 1) 
    If data(i, 2) <> dict1(data(i, 1)) Then 
     result(j, 1) = data(i, 1) 
     result(j, 2) = dict1(data(i, 1)) 
     result(j, 3) = data(i, 2) 
     result(j, 4) = data(i, 3) 
     j = j + 1 
    End If 
Next i 

With Sheets("Sheet2") 
    .Cells(1, 5).Resize(UBound(result, 1), UBound(result, 2)) = result 
End With 

結束子

+0

我做了一些研究,並指出,「字典「在這個代碼中使用的對象不支持多維度,我們是否應該將它重新做爲一個數組呢? – Dman 2012-03-14 02:50:37

+0

This c應該是一個解決方案。你可以在這些線程中找到一些靈感:http://stackoverflow.com/questions/4873182/so​​rting-a-multidimensionnal-array-in-vba和http://stackoverflow.com/questions/152319/vba-array-sort功能 – JMax 2012-03-14 07:49:13

回答

1

我評論的代碼,並修改它來獲得6列,現在是一個快速射擊這樣大概可以改進,優化等

Public Sub doIt() 

    Dim inputData As Variant 
    Dim result As Variant 
    Dim thisGroup As String 
    Dim thisMember As String 
    Dim thisScore As String 
    Dim i As Long 
    Dim j As Long 
    Dim membersWithHighestScore As Variant 'Will store the member with highest score for each group 
    Dim highestScore As Variant 'Will store the highest score for each group 

    Set membersWithHighestScore = CreateObject("Scripting.Dictionary") 
    Set highestScore = CreateObject("Scripting.Dictionary") 
    inputData = Sheets("Sheet1").UsedRange 

    'First step: populate the dictionaries 
    'At the end of the loop: 
    ' - membersWithHigestScore will contain the member with the highest score for each group, for example: X=B, Y=B, ... 
    ' - highestScore will contain for example: X=7, Y=11, ... 
    For i = LBound(inputData, 1) To UBound(inputData, 1) 
     thisGroup = inputData(i, 1) 'The group for that line (X, Y...) 
     thisMember = inputData(i, 2) 'The member for that line (A, B...) 
     thisScore = inputData(i, 3) 'The score for that line 
     If membersWithHighestScore.exists(thisGroup) Then 'If there already is a member with a high score in that group 
      If highestScore(thisGroup) < thisScore Then 'if this new line has a higher score 
       membersWithHighestScore(thisGroup) = thisMember 'Replace the member with highest score for that group with the current line 
       highestScore(thisGroup) = thisScore 'This is the new highest score for that group 
      End If 'If the line is not a new high score, skip it 
     Else 'First time we find a member of that group, it is by definition the highest score so far 
      membersWithHighestScore(thisGroup) = thisMember 
      highestScore(thisGroup) = thisScore 
     End If 
    Next i 

    ReDim result(LBound(inputData, 1) To UBound(inputData, 1) - membersWithHighestScore.Count, 1 To 7) As Variant 

    j = 1 
    For i = LBound(inputData, 1) To UBound(inputData, 1) 
     thisGroup = inputData(i, 1) 'The group for that line (X, Y...) 
     thisMember = inputData(i, 2) 'The member for that line (A, B...) 
     If thisMember <> membersWithHighestScore(thisGroup) Then 'If this is a line containing the highest score for that group, skip it 
      result(j, 1) = thisGroup 
      result(j, 2) = membersWithHighestScore(thisGroup) 
      'Copy the rest of the data as is 
      result(j, 3) = inputData(i, 2) 
      result(j, 4) = inputData(i, 3) 
      result(j, 5) = inputData(i, 4) 
      result(j, 6) = inputData(i, 5) 
      result(j, 7) = inputData(i, 6) 
      j = j + 1 
     End If 
    Next i 

    With Sheets("Sheet2") 
     .Cells(1, 5).Resize(UBound(result, 1), UBound(result, 2)) = result 
    End With 

End Sub 
+0

這是一個非常明確和非常有用的代碼。這完全回答了我的問題,併爲我提供了一個學習如何構建這樣的邏輯的平臺 - 我真誠地感謝你。 – Dman 2012-03-15 03:39:31