あなたは、この(コメント)コード試すことができます:コラム「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
これは自分で行ったことがありますか?もしそうなら、今まで持っていたコードを投稿できますか? – bobajob
さらにコードをお願いしますか? –