Combine Rows With the Same Unique ID but Keep Them Separated by Month

goob90

New Member
Joined
Nov 12, 2021
Messages
34
Office Version
  1. 365
Platform
  1. Windows
I have employee data broken down by employee name, id, deduction month, deduction code, and deduction amount. I want to use VBA to combine the rows with the same employee id, list the deduction codes, and total the deduction amount. I want to do this but leave it separated by deduction month. Could someone point me in the right direction to do this?

1653261805132.png
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

I want to use VBA to combine the rows with the same employee id, list the deduction codes, and total the deduction amount.
I didn't understand why in your sample Janine only has "MPHO" in the 4th result column but see if this does what you want.

VBA Code:
Sub Rearrange()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  Dim s As String
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("E" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, Array(1, 2, 3)), ";") & ";"
    If Not d.exists(s) Then d(s) = s & ";0"
    d(s) = s & Split(d(s), ";")(3) & "," & a(i, 4) & ";" & (Split(d(s), ";")(4) + a(i, 5))
  Next i
  Application.ScreenUpdating = False
  With Range("G2").Resize(d.Count)
    .Value = Application.Transpose(d.items)
    .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False
    .Offset(, 3).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(1, 1))
    .CurrentRegion.Columns.AutoFit
  End With
  Application.ScreenUpdating = True
End Sub

Here is my sample data and the result of the above code.

goob90.xlsm
ABCDEFGHIJK
1
2a15X1a15X,Q,X6
3a15Q2a14X,Q,X15
4a15X3b25Y,Q,Y24
5a14X4b24Y,Q,Y33
6a14Q5
7a14X6
8b25Y7
9b25Q8
10b25Y9
11b24Y10
12b24Q11
13b24Y12
14
Sheet1
 
Upvote 0
Solution
I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.


I didn't understand why in your sample Janine only has "MPHO" in the 4th result column but see if this does what you want.

VBA Code:
Sub Rearrange()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  Dim s As String
 
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("E" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, Array(1, 2, 3)), ";") & ";"
    If Not d.exists(s) Then d(s) = s & ";0"
    d(s) = s & Split(d(s), ";")(3) & "," & a(i, 4) & ";" & (Split(d(s), ";")(4) + a(i, 5))
  Next i
  Application.ScreenUpdating = False
  With Range("G2").Resize(d.Count)
    .Value = Application.Transpose(d.items)
    .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False
    .Offset(, 3).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(1, 1))
    .CurrentRegion.Columns.AutoFit
  End With
  Application.ScreenUpdating = True
End Sub

Here is my sample data and the result of the above code.

goob90.xlsm
ABCDEFGHIJK
1
2a15X1a15X,Q,X6
3a15Q2a14X,Q,X15
4a15X3b25Y,Q,Y24
5a14X4b24Y,Q,Y33
6a14Q5
7a14X6
8b25Y7
9b25Q8
10b25Y9
11b24Y10
12b24Q11
13b24Y12
14
Sheet1
Thank you so much! That worked perfectly!

The reason Janine only had MPHO in the 4th column is that I forgot to add the MPB and other MPHO to it. Also, I will check out XL2BB,

Again, thank you so much!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
The code is still working great! I have run into an issue with it when running with large amounts of data. I am running it with around 26,000 rows and it is freezing up and ultimately Excel shows "Not Responding". Do you have any advice?
 
Upvote 0
Have you investigated the Power Pivot solution I suggested?
Yes! Thank you for following up! I've actually used Power Pivot for a report like this several times. I am only using a MACRO because I had a button connected to it and it makes it easy for some of my coworkers who are not computer savvy able to run the report a little easier.
 
Upvote 0
Just wondering, in the desired format, do you want/need repeated items in the deduction code column like below or would "MPLO" listed just once be just as good?

1660352067197.png
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,654
Latest member
mememe101

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