Macro to fill an identifier down and SUMIF

xbrianx

New Member
Joined
Dec 2, 2010
Messages
24
I have extracted some data from our employee database. It's 10,000+ rows. The first row of the employee is their name, a hypen, then their 9-digit SSN (11-digits including the two hyphens in the SSN). After each employee is 2-7 rows of deductions. The last row of the employee has the total amount. In column 'B there is the individual deduction name (irrelevant) and in column 'C' is the individual deduction amount - see attached image. I need to create a new tab that extracts the SSN in one column with the SUM of all their deductions in the next column. I tried using =RIGHT(A2, 11) to extract only the SSN, but I only want to copy/paste that formula for the employee names (cells A2, A5, A10, etc.). I then tried to use a SUMIF formula on column 'C' thinking that would be easier to get versus trying to pull the last row's total amount highlighted in yellow. Can you help?
 

Attachments

  • Screenshot_8.png
    Screenshot_8.png
    41.3 KB · Views: 14

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
To extract the SSN, if your data starts from row 2, you can use this formula in cell D2 and drag it down:

Excel Formula:
 =IF(MOD(ROW()-2,4)=0,RIGHT(A2,11),"")
This formula will only extract the SSN for every 4th row (since you mentioned the data has 2-7 rows of deductions between each employee).

To calculate the sum of deductions for each SSN, In cell E2, you can use the following formula and drag it down:
Excel Formula:
=IF(D2<>"",SUMIF($D$2:$D$10000,D2,$C$2:$C$10000),"")
This formula will sum the deductions in column C for each SSN in column D.

Make sure to adjust the range ($D$2:$D$10000 and $C$2:$C$10000) in the formulas to match your data. Also, adjust the row numbers accordingly if your data doesn't start from row 2.
 
Upvote 0
@shina67 you have already been warned about using AI to answer questions. If you continue you will get a ban.
 
Upvote 0
Would have been better if you had used the MrExcel XL2BB add-in and posted what you actually have instead of a picture of it.
Any ways, with the test workbook I setup from your picture, this macro using a dictionary, seems to provide the results you're after.
VBA Code:
Sub Testing()
   
    Dim lr As Long, i As Long
    Dim dic As Object, arr As Variant
    Dim SSN As String, ssn2 As String, item As Double
   
Set dic = CreateObject("Scripting.Dictionary")

With Sheets("Sheet1")
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    arr = .Range("A2:A" & lr)
End With

For i = 1 To UBound(arr)
    If i = 1 Then
        SSN = Right(arr(1, LBound(arr)), 11)
        item = 0
    End If
   
    If i > 1 And i < UBound(arr) Then
        If Len(arr(i, 1)) > 12 Then
            ssn2 = Right(arr(i, 1), 11)
            If ssn2 <> SSN Then
                item = arr(i - 1, 1)
            End If
        End If
    End If
   
    If i = UBound(arr) Then
        item = arr(i, 1)
    End If
   
    If item <> 0 Then
        dic.Add SSN, item
        SSN = ssn2
        item = 0
    End If
Next i

Worksheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet
    .Name = "Results"
    .Range("A1") = "Employee"
    .Range("A2").Resize(dic.Count) = Application.Transpose(dic.keys)
    .Range("B1") = "Total Amount"
    .Range("B2").Resize(dic.Count) = Application.Transpose(dic.items)
    .Columns.AutoFit
End With

End Sub
 
Upvote 0
Would have been better if you had used the MrExcel XL2BB add-in and posted what you actually have instead of a picture of it.
Any ways, with the test workbook I setup from your picture, this macro using a dictionary, seems to provide the results you're after.
VBA Code:
Sub Testing()
  
    Dim lr As Long, i As Long
    Dim dic As Object, arr As Variant
    Dim SSN As String, ssn2 As String, item As Double
  
Set dic = CreateObject("Scripting.Dictionary")

With Sheets("Sheet1")
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    arr = .Range("A2:A" & lr)
End With

For i = 1 To UBound(arr)
    If i = 1 Then
        SSN = Right(arr(1, LBound(arr)), 11)
        item = 0
    End If
  
    If i > 1 And i < UBound(arr) Then
        If Len(arr(i, 1)) > 12 Then
            ssn2 = Right(arr(i, 1), 11)
            If ssn2 <> SSN Then
                item = arr(i - 1, 1)
            End If
        End If
    End If
  
    If i = UBound(arr) Then
        item = arr(i, 1)
    End If
  
    If item <> 0 Then
        dic.Add SSN, item
        SSN = ssn2
        item = 0
    End If
Next i

Worksheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet
    .Name = "Results"
    .Range("A1") = "Employee"
    .Range("A2").Resize(dic.Count) = Application.Transpose(dic.keys)
    .Range("B1") = "Total Amount"
    .Range("B2").Resize(dic.Count) = Application.Transpose(dic.items)
    .Columns.AutoFit
End With

End Sub
@NoSparks - thanks for the reply. Sorry I was unable to use the MrExcel XL2BB add-in as it is blocked on our network. This macro works very well. How'd you do it? :)
-Brian
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top