状況:最初の列の値(アイテムID番号)に基づいて行を合計することで、統合しようとしているデータがあります。 ID番号が一致する場合は、行を一緒に追加し、重複行を削除します。連結ループで間違った行が削除されました
私は次のコードを書いており、2つの問題が発生しています:1.コードを実行するときに、常に残りの重複が残っています。2.コードを再実行すると、行が合計され、それらが重複していなくても。
ご協力いただければ幸いです。
Sub ConsolidateRows()
Dim WB As Workbook
Dim WS As Worksheet
Dim iRow As Long
Dim iCol As Long
Dim LastRow As Long
Dim LastCol As Long
Dim duplicate As String
Dim dupRow As Long
Dim cell As Range
Dim i As Integer
'set
Set WB = Workbooks("Book1")
Set WS = WB.Sheets("Data")
LastRow = WS.UsedRange.Rows.Count
LastCol = WS.UsedRange.Columns.Count
'Loop to consolidate, delete the duplicate rows
iRow = 1
While WS.Cells(iRow, 1).Value <> ""
duplicate = Cells(iRow, 1).Value
iRow = iRow + 1
For Each cell In WS.Range("A1:A" & LastRow).Cells
dupRow = cell.Row
If cell.Value = duplicate And iRow <> dupRow Then
For iCol = 3 To LastCol
Cells(iRow, iCol) = Application.WorksheetFunction.Sum(Cells(iRow, iCol), Cells(dupRow, iCol))
Next iCol
WS.Rows(dupRow).Delete
End If
Next cell
Wend
End Sub
あなたは 'SUMIF'式とExcelリボンに組み込まれた' RemoveDuplicates'機能を使うことができます。または、実際に自動化が必要な場合は、単にVBAを作成してください。 –