GeorgeTimes
New Member
- Joined
- Jul 22, 2022
- Messages
- 16
- Office Version
- 365
- 2021
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
- Platform
- Windows
Hi Guys,
I need your help with my vba code. I need to locate the duplicates from column A, leave just 1 row of that column and add the sum of column N (the rest of the columns don't matter).
Below it's an example of my raw data (A column and N column)
Above we have 2 duplicates, 102099147 (2x duplicate) and 102099146 (3x duplicate). Need the vba code to result like below:
The below vba works and does that, however, if there's a blank cell in column A (see first example, no account number, but value in column N - 36.56), the loop in vba will be an infinite loop. I need your help to fix this as I can't figure it out.
Sub ReorgDataSumCount()
Dim r As Long, lr As Integer, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "G").End(xlUp).Row
Range("A2:AG" & lr).Sort key1:=Range("A2"), order1:=2
For r = 2 To lr
n = Application.CountIf(Columns(1), Cells(r, 1).Value)
If n = 1 Then
Range("AH" & r) = 0
ElseIf n > 1 Then
Range("AH" & r) = n - 1
Range("N" & r).Value = Evaluate("=Sum(N" & r & ":N" & r + n - 1 & ")")
Range("A" & r + 1 & ":A" & r + n - 1) = ""
End If
r = r + n - 1
Next r
On Error Resume Next
Range("A2:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Columns("A:AG").AutoFit
Columns("AH:AH").ClearContents
Application.ScreenUpdating = True
End Sub
I need your help with my vba code. I need to locate the duplicates from column A, leave just 1 row of that column and add the sum of column N (the rest of the columns don't matter).
Below it's an example of my raw data (A column and N column)
Account number | Amount | |
102105709 | 82.84 | |
102099147 | 27.36 | |
| 73.13 | |
| 54.72 | |
102099146 | 73.13 | |
102099146 | 54.72 | |
| 36.56 |
Above we have 2 duplicates, 102099147 (2x duplicate) and 102099146 (3x duplicate). Need the vba code to result like below:
Account number | Amount | ||
| 82.84 | ||
102099147 | 100.49 | ||
| 182.57 |
The below vba works and does that, however, if there's a blank cell in column A (see first example, no account number, but value in column N - 36.56), the loop in vba will be an infinite loop. I need your help to fix this as I can't figure it out.
Sub ReorgDataSumCount()
Dim r As Long, lr As Integer, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "G").End(xlUp).Row
Range("A2:AG" & lr).Sort key1:=Range("A2"), order1:=2
For r = 2 To lr
n = Application.CountIf(Columns(1), Cells(r, 1).Value)
If n = 1 Then
Range("AH" & r) = 0
ElseIf n > 1 Then
Range("AH" & r) = n - 1
Range("N" & r).Value = Evaluate("=Sum(N" & r & ":N" & r + n - 1 & ")")
Range("A" & r + 1 & ":A" & r + n - 1) = ""
End If
r = r + n - 1
Next r
On Error Resume Next
Range("A2:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Columns("A:AG").AutoFit
Columns("AH:AH").ClearContents
Application.ScreenUpdating = True
End Sub