如何自定义排序和添加单元格?

时间:2016-10-07 11:52:58

标签: excel vba

我有一张带有数百条记录的excel表。专栏" C"具有该特定帐户的名称。专栏" D"具有应该应用于该帐户的金额。 *注意栏" C"可以有重复的值。

专栏" M"列表中列出了所有可能出现在列" C"和列" N"有一个数字1 - ????按顺序将这些项目添加到sheet2中后进行分组。

例如:

Column C Column D                 Column M   Column N
John      $500                    John       1 
Jane     -$600                    Jane       2 
Jack      $250                    Jack       3
Jane      $45.00                  Joe        4
Joe                               Jay        5
Jack      $0.00                   Jayson     6
Jay       $85                     Jill       7

表2应该按照以下列的顺序排列上面的数据" N"并添加了

2 个答案:

答案 0 :(得分:0)

对我来说就像一个Pivot-Table。 第一列中的编号只是一个" IF" s的链 数据源的透视图是:Tabelle1!$ B $ 4:$ D $ 10 enter image description here

e.g。 B5:=IF(C5=$F$5;$G$5;IF(C5=$F$6;$G$6;IF(C5=$F$7;$G$7;IF(C5=$F$8;$G$8;IF(C5=$F$9;$G$9;IF(C5=$F$10;$G$10;IF(C5=$F$11;$G$11;"")))))))

答案 1 :(得分:0)

你可以尝试这个(评论过的)代码:

Option Explicit

Sub main()
    Dim cell As Range, namesRng As Range, dataRng As Range

    With Worksheets("Sheet1") '<-- reference "Sheet1" (change "Sheet1" to your actual data sheet name)
        Set dataRng = .Range("C1", .Cells(.Rows.Count, "C").End(xlUp)).Resize(, 2) '<--| set the data range from cell "C1" to column "D" cell in column "C" last non empty row
        With .Range("M1", .Cells(.Rows.Count, "M").End(xlUp)).Resize(, 2)  '<-- reference names/order range from cell "M1" to column "N" cell in column "M" last non empty row
            .Sort key1:=.Range("B1"), order1:=xlAscending, Header:=xlNo '<-- sort names in column "M" by column "N" order number
            Set namesRng = .Columns(1).Cells '<-- set ordered names range
        End With
    End With

    With Worksheets("Sheet2") '<-- reference "Sheet2" (change "Sheet2" to your actual output sheet name)
        For Each cell In namesRng '<-- loop through ordered name range
            .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2).Value = Array(cell.Value, WorksheetFunction.SumIf(dataRng.Columns(1), cell.Value, dataRng.Columns(2))) '<--| write current name in Sheet2 column "A" current empty cell after last not empty one, and corresponding sum in adjacent cell
        Next cell
    End With
End Sub

如果列中的名称&#34; M&#34;总是已经正确订购,然后没有必要按列排序&#34; N&#34;并且代码略微简化为:

Option Explicit

Sub main()
    Dim cell As Range, namesRng As Range, dataRng As Range

    With Worksheets("Sheet1") '<-- reference "Sheet1" (change "Sheet1" to your actual data sheet name)
        Set dataRng = .Range("C1", .Cells(.Rows.Count, "C").End(xlUp)).Resize(, 2) '<--| set the data range from cell "C1" to column "D" cell in column "C" last non empty row
        Set namesRng = .Range("M1", .Cells(.Rows.Count, "M").End(xlUp)) '<-- set ordered names range
    End With

    With Worksheets("Sheet2") '<-- reference "Sheet2" (change "Sheet2" to your actual output sheet name)
        For Each cell In namesRng '<-- loop through ordered name range
            .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2).Value = Array(cell.Value, WorksheetFunction.SumIf(dataRng.Columns(1), cell.Value, dataRng.Columns(2))) '<--| write current name in Sheet2 column "A" current empty cell after last not empty one, and corresponding sum in adjacent cell
        Next cell
    End With
End Sub