2016-10-07 12 views
0

私は何百ものレコードを持つ優れたシートを持っています。列「C」には、その特定のアカウントの名前があります。列「D」はその口座に適用されるべき金額です。 *注記列 "C"は繰り返し値を持つことができます。カスタムソートとセルの追加方法は?

列「M」には、列「C」に表示される可能性のあるすべての名前のリストがあり、列「N」には番号1が付いています。これらの項目はシート2に追加された後にグループ化されるべきである。

例:

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」の順に入れ、上記のデータを持っていると

+1

これは自分で行ったことがありますか?もしそうなら、今まで持っていたコードを投稿できますか? – bobajob

+0

さらにコードをお願いしますか? –

答えて

0

が私にピボットテーブルのように見える追加する必要があります。 !Tabelle1 $ B $ 4:$ D $ 10 enter image description here

例えば最初の列の 番号は、ピボットのS データ・ソースは "IF" のちょうど鎖であり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;"")))))))

0

あなたは、この(コメント)コード試すことができます:コラム「M」の名前は、常にすでに適切に順序付けられている場合

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 

を、その列「N」によってそれらをソートする必要はありませんし、コードは少し簡略化されます:

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 
関連する問題