2012-03-14 153 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