Excel VBA: Combine Row Data of Specific Accounts

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

Good day!

I just want to consult if there's a VBA way to combine the data (get the sum) of specific accounts?


For example I have long list of data but need to combine the total data for Math & Science. Note that there are negative numbers sometimes and Reference 01 is the main account.

Initial Data:


ReferenceNameData 1Data 2Data 3Data 4Data 5Data 6
01Math10075609095-100
02Science80758090-8580

Output Data: Since Reference No 1 is the main account, this is the surviving account after merging. The name will be renamed as Math & Science after combining the data. Afterwards, this row will be highlighted in yellow cell for easy tracking of this account.

ReferenceNameData 1Data 2Data 3Data 4Data 5Data 6
01 Math & Science18015014018010-20


Appreciate the help.

Thanks!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
select both rows of data and run such code:
VBA Code:
Sub test()

Dim i As Long
If Selection.Rows.Count <> 2 Or Selection.Columns.Count < 3 Then
  MsgBox "select at least 3 columns and 2 consecutive rows"
  Exit Sub
End If
Selection.Cells(1, 2) = Selection.Cells(1, 2) & " & " & Selection.Cells(2, 2)
For i = 3 To Selection.Columns.Count
  Selection.Cells(1, i) = Selection.Cells(1, i) + Selection.Cells(2, i)
Next i
Selection.Cells(2, 1).Resize(1, Selection.Columns.Count).ClearContents ' one could use here:   Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp  

End Sub
 
Upvote 0
select both rows of data and run such code:
VBA Code:
Sub test()

Dim i As Long
If Selection.Rows.Count <> 2 Or Selection.Columns.Count < 3 Then
  MsgBox "select at least 3 columns and 2 consecutive rows"
  Exit Sub
End If
Selection.Cells(1, 2) = Selection.Cells(1, 2) & " & " & Selection.Cells(2, 2)
For i = 3 To Selection.Columns.Count
  Selection.Cells(1, i) = Selection.Cells(1, i) + Selection.Cells(2, i)
Next i
Selection.Cells(2, 1).Resize(1, Selection.Columns.Count).ClearContents ' one could use here:   Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp 

End Sub
Hi Kaper,

Thanks a lot for sharing which works however, I need to do the same for the next 5 tab sheets. I was thinking of looping it but have some questions:

1. If the rows to merge is permanently settled in the Row 2 & 3, can we do that instead of selecting rows?
2. If I want to merge the data up to Column H only, which part of the code are you going to edit?

Appreciate the help as always. :)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

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