0

所以我试图对每个名字都有最喜欢的颜色的名字列表进行排序。换句话说,我希望排序类似于以下示例:(A 和 B 对应列,而 # 对应行)

  **A**  **B**                  **A**   **B**
1 Tim    Red                  1 Josh    Black
2        Blue                 2         Yellow
3        Purple               3 Maria   Grey 
4 Josh   Yellow               4         Orange
5        Black                5         Pink
6 Maria  Pink                 6 Tim     Blue
7        Orange               7         Purple
8        Grey                 8         Red

我希望它首先对名称进行排序,无论该名称走到哪里,颜色都会跟随其位置,然后对颜色进行排序。有没有办法在不使用 VBA 的情况下做到这一点,因为我不知道如何使用 VBA。任何帮助将不胜感激,并且为了记录,这不是课堂作业。

我目前正在使用 Microsoft Excel 2011 for Mac。

4

2 回答 2

0

This can be done without VBA, using helper columns and a manual sort.

If you want to do this all the time instead of just once, you may want to consider changing your data architecture. Entering the name in a raw data sheet on every row, then build a pivot table that shows the sorted layout you are after.

Steps to do the sort manually:

Insert a header row in row 1 and put in labels for each column, i.e. Name, color. Add another column and use this formula in cell C2 (copy down):

=IF(ISBLANK(A2),C1,A2)

Copy column C and paste over itself with Paste Special > Values. Now there are the names only in this column. Use the sort dialog to sort by the new column first and by the color second. These are the settings before confirming the sort:

enter image description here

After the sort, you will see this:

enter image description here

Add another formula column with this formula starting in D2 and copied down:

=IF(C2<>C1,C2,"")

Copy column D, paste as values over column A. Delete the helper columns.

于 2014-05-28T01:07:46.540 回答
0

我对工作表函数和东西不是那么好,但我认为不使用 VBA 就无法实现你想要的。

假设 Mac VBA 与 Windows 上的相同,以下代码应该可以帮助您入门。

想法:通过填充空白的“名称”单元格来进行常规排序,一旦排序完成,删除多余的名称。我没有包含进行实际排序的代码,但下面的两种方法应该填充空单元格并将它们清空。

Public Sub InsertDuplicates()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet1")
Dim Current As String: Current = ""
Dim Row As Integer: Row = 1
' Fill the blank cells in the names column
Do
    If Sheet.Cells(Row, 2).Value2 = "" Then
        ' Break out of the loop
        Exit Do
    End If
    If Sheet.Cells(Row, 1).Value2 = "" Then
        ' No name present, so populate cell with the current name
        Sheet.Cells(Row, 1).Value2 = Current
    Else
        ' A name has been found, set it as the current name
        Current = Sheet.Cells(Row, 1).Value2
    End If
    ' Goto the next row
    Row = Row + 1
Loop

End Sub

Public Sub RemoveDuplicates()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet1")
Dim Current As String: Current = ""
Dim Row As Integer: Row = 1
' Remove unwanted duplicate names in names column
Do
    If Sheet.Cells(Row, 2).Value2 = "" Then
        ' Break out of the loop
        Exit Do
    End If
    If Sheet.Cells(Row, 1).Value2 = Current Then
        ' Row is a duplicate so empty the value
        Sheet.Cells(Row, 1).Value2 = ""
    Else
        ' Row is different from the previous, store the value and continue
        Current = Sheet.Cells(Row, 1).Value2
    End If
    ' Goto the next row
    Row = Row + 1
Loop

End Sub

Public Sub SortList()
' perform the sort (you can record a macro to get the code required)
End Sub

Public Sub DoIt()
' this is the main macro, call this sub to action a sort.
InsertDuplicates
SortList
RemoveDuplicates
End Sub
于 2014-05-28T00:09:23.537 回答