how to add/sum two or more rows with similar cell value then put the result on the last row and delete the other similar rows (in VBA)?

gzesasaki

New Member
Joined
Jul 18, 2022
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Dear Sir/Madam,

can you please help me in resolving my issue on excel and what i am trying to do is to add/sum two or more rows with similar cell value then put the result on the last row (or overwrite the first row with the cell value being summed) and then delete the other rows with cell value being summed.

i am attaching an excel png as sample..
sample.png


" two rows with same value on coumn D ("WALK IN") then cell G should be added. And Im attaching also the required result..

result.png


thank you very much in advance..
gzesasaki
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Colums J, K, N have to be summed..
Thank you.
Try this with a copy of your data.

VBA Code:
Sub Combine_Rows()
  Dim r As Long
  
  Application.ScreenUpdating = False
  Range("A5", Range("N" & Rows.Count).End(xlUp)).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlNo
  For r = Range("A" & Rows.Count).End(xlUp).Row To 6 Step -1
    If Range("D" & r).Value = Range("D" & r - 1).Value Then
      Range("L" & r).Resize(, 3).Copy
      Range("L" & r - 1).PasteSpecial Operation:=xlAdd
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you.
Try this with a copy of your data.

VBA Code:
Sub Combine_Rows()
  Dim r As Long
 
  Application.ScreenUpdating = False
  Range("A5", Range("N" & Rows.Count).End(xlUp)).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlNo
  For r = Range("A" & Rows.Count).End(xlUp).Row To 6 Step -1
    If Range("D" & r).Value = Range("D" & r - 1).Value Then
      Range("L" & r).Resize(, 3).Copy
      Range("L" & r - 1).PasteSpecial Operation:=xlAdd
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
thank you sir for the code.

the data are sorting according to column perfectly but rows with the same value in column D are not adding/combining and so columns J,K N are not adding too.

regards,
gzesasaki
 
Upvote 0
rows with the same value in column D are not adding/combining and so columns J,K N are not adding too.
They are for me. First thing to check is for two items that look the same in column D but are not combining, check that say one of them has a space character at the end and one does not.

But in any case also make this change in the code.

Rich (BB code):
Sub Combine_Rows()
  Dim r As Long
  
  Application.ScreenUpdating = False
  Range("A5", Range("N" & Rows.Count).End(xlUp)).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlNo
  For r = Range("A" & Rows.Count).End(xlUp).Row To 6 Step -1
    If Range("D" & r).Value = Range("D" & r - 1).Value Then
      Range("L" & r).Resize(, 3).Copy
      Range("L" & r - 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
They are for me. First thing to check is for two items that look the same in column D but are not combining, check that say one of them has a space character at the end and one does not.

But in any case also make this change in the code.

Rich (BB code):
Sub Combine_Rows()
  Dim r As Long
 
  Application.ScreenUpdating = False
  Range("A5", Range("N" & Rows.Count).End(xlUp)).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlNo
  For r = Range("A" & Rows.Count).End(xlUp).Row To 6 Step -1
    If Range("D" & r).Value = Range("D" & r - 1).Value Then
      Range("L" & r).Resize(, 3).Copy
      Range("L" & r - 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
hi sir,
i have check and it is something to do with the string in column D, now the other rows with the same value are getting deleted but the values on column J, K and N are not adding.

thank you in advance again sir..
 
Upvote 0
The code from post #15 worked for me with the sample data from post #5. Are you saying that is not the case for you?

If it works for you for that particular sample but not some other data then please provide a small set of XL2BB sample data where it does not work for you.
 
Upvote 0
The code from post #15 worked for me with the sample data from post #5. Are you saying that is not the case for you?

If it works for you for that particular sample but not some other data then please provide a small set of XL2BB sample data where it does not work for you.
hi sir please find the attached

RELIEF sales for test.xlsm
ABCDHIJKLMNOP
2
3tax_monthseq_notinregistered_nameaddress1address2 gsales gtsales gesales gzsales tax_rate touttax
4Tax MonthSeq. NoTIN NumberRegistered Name (For Non-Individuals)Substreet, Street, BrgyDistrict, City, Zip Code Gross Taxable Total Sales Exempt Zero-Rated Taxable (Net of VAT) Tax Rate Total Output Tax
531/05/20221241556496000ANSON @HOME INC.#23 ADB AVE.ORTIGAS CENTER PASIG CITY74,710.6774,710.67---12.008,965.28
631/05/20222000106840000ANSON EMPORIUM CORPORATIONALABANG TOWN CENTER-LANDMARK BLDG.,MAKATI AVENUE, AYALA CENTER, MAKATI CITY56,350.0856,350.08---12.006,762.01
731/05/20223006899069000ANSON MARKETING AND COMMERCIAL INC.2ND FLOOR CASH & CARRY MALLFILMORE ST.,MAKATI CITY93,207.1793,207.17---12.0011,184.86
831/05/20224000106840000ANSON'S EMPORIUM CORPORATIONANSON MAKATI -LANDMARK BLDG.,MAKATI AVENUE, AYALA CENTER, MAKATI CITY43,892.8343,892.83---12.005,267.14
931/05/20225158810907000AUDIBLE ILLUSIONS ENTERPRISE50 D. TIMOG AVENUE, QUEZON CITY0.000.00---12.000.00
1031/05/20226220363969000AUDIO WORLD TECHNOLOGY INC.749 BANAWE AVE. CORNER SCOUT ALCARAZQUEZON CITY20,678.5820,678.58---12.002,481.43
1131/05/20227AVX AUDIO SYSTEMSEA SCAPE VILLAGEATANG DELA RAMA ST.0.000.00---12.000.00
1231/05/20228BETTER COMPONENT INCORPORATEDJUAN LUNA ST. BRGY 29-C POBLACION DISTRICT DAVAO CITY 8000206,785.75206,785.75---12.0024,814.29
1331/05/202212COOL PLANET(0.00)(0.00)---12.00(0.00)
1431/05/202213000124374HONDA CARS PHILS. INC. LAGUNA105 SOUTH MAIN AVENUE LAGUNATECHNOPARK STA. ROSA LAGUNA0.000.00---12.000.00
1531/05/202214240570218000JFK POWER SALES & CONSTRUCTION43 Garnet St. Posadas VillageBgy. Sucat Muntinlupa City974,852.67974,852.67---12.00116,982.32
1631/05/202215005589352000KEVIN KARLO CORP.1518 Felix Huertas St. Sta. Cruz ManilaSt. Sta. Cruz Manila1,095,334.831,095,334.83---12.00131,440.18
1731/05/202216SOUND GALLERYMAKATI CITY(0.00)(0.00)---12.00(0.00)
1831/05/202217000086204000STAR APPLIANCE CENTER INC.-BACOORBACOOR CAVITEBACOLOD CITY48,000.0048,000.00---12.005,768.78
1931/05/202219000086204000STAR APPLIANCE CENTER INC.-BATANGASBRGY PALIOCAN WEST BATANGASCITY(2,331.67)(2,331.67)---12.00(279.80)
2031/05/202223000086204000STAR APPLIANCE CENTER INC.-DAVAO LANANGPremiere J.P. Laurel Ave., Brgy San AntonioAgdao Dist.,Davao City Davao Del Sur134,478.83134,478.83---12.0016,137.46
2131/05/202224000086204000STAR APPLIANCE CENTER INC.-SOUTHMALLZAPOTE ROAD LAS PINAS120,424.33120,424.33---12.0014,450.92
2231/05/202225000086204000STAR APPLIANCE CENTER INC.-MALL OF ASIA1103-1104 GRD.FLR.SM MOA JW DIOKNODPT-1A PASAY CITY66,474.5866,474.58---12.007,976.95
2331/05/202226000086204000STAR APPLIANCE CENTER INC.-MANILACONCEPCION COR. ARROCEROS ANDSAN MARCELINO ST. BESIDE43,702.1743,702.17---12.005,244.26
2431/05/202227000086204000STAR APPLIANCE CENTER INC.-MEGA ASM MEGAMALL BLDG. COR. EDSABRGY. WACK-WACK, MANDALUYONG CITY0.000.00---12.000.00
2531/05/202228000086204000STAR APPLIANCE CENTER INC.-MEGA BSM MEGAMALL BLDG. EDSA COR. DOÑAJULIA VARGAS AVENUE WACK-WACK GREENHILLS,CITY OF380,680.50380,680.50---12.0045,681.66
2631/05/202229000086204000STAR APPLIANCE CENTER INC.-N.E. IIISM NORTH EDSA LOWER GRD. FLOOR33,740.7533,740.75---12.004,048.89
2731/05/202230000086204000STAR APPLIANCE CENTER INC.-PAMPANGASAN FERNANDO PAMPANGA64,887.0864,887.08---12.007,786.45
2831/05/202231000086204000STAR APPLIANCE CENTER INC.-PASIG8012 E. RODRIQUEZ JR.AVE., BRGY UGONG PASIG CITY(9,375.00)(9,375.00)---12.00(1,125.00)
2931/05/202232000086204000STAR APPLIANCE CENTER INC.-SAN LAZAROFELIX HUERTAS COR.A.H.LACSONSTA. CRUZ MANILA86,821.5886,821.58---12.0010,418.59
3031/05/202233000086204000STAR APPLIANCE CENTER INC.-SEASIDE CEBUSM CITY CEBU S.R.P.GF SM CITY SEASIDE MAMBALING ROAD,SOUTH ROAD34,129.5034,129.50---12.004,095.54
3131/05/202234000086204000STAR APPLIANCE CENTER INC.-SOUTHMALLZAPOTE ROAD LAS PINAS42,948.9242,948.92---12.005,153.87
3231/05/202235010128681000SYMPHONY AUDIO VIDEO CORP.UNIT 3023, 3RD FLR. SM SEASIDE CITY CEBU,MAMBALING, CEBU CITY 6000403,232.17403,232.17---12.0048,387.86
3331/05/20223600041571300000TOYOTA MOTOR PHILIPPINES CORPORATIONToyota Special Economic Zone,Sta Rosa-Tagaytay Highway, Sta Rosa City Laguna0.000.00---12.00-
3431/05/202237005-437-336UNITED EVANGELICAL CHURCH OF LEGASPILEGASPI, ALBAY103,660.75103,660.75---12.0012,439.29
3531/05/202238WALK IN0.000.00---12.00-
3631/05/202239223359036007WESTERN GRAND CENTRAL CO.,INC. - FESTIVAL3RD FLOOR,FILINVEST CORPORATE CITYALABANG,MUNTINLUPA CITY 1770437,547.08437,547.08---12.0052,505.65
3731/05/202240223359036006WESTERN GRAND CENTRAL CO.,INC. - STA LUCIAPHASE 3 BASEMENT LEVEL STA.LUCIAEAST GRAND MALL MARCOS HIGHWAY309,874.08309,874.08---12.0037,184.89
3831/05/20221HONDA PHILIPPINES INCTANAUAN CITY, BATANGAS10,267.8310,267.83---12.001,232.14
3931/05/20222LOYOLA HOUSE OF STUDIESATENEO DE MANILA UNIVERSITY,1,339.251,339.25---12.00160.71
4031/05/20223WALK IN29,062.0829,062.08---12.003,487.45
Sales
 
Upvote 0
Looks like the layout of the data has changed since the beginning when it went from column A to column N.
Now it goes to column P. :)

Can you confirm which columns need to be added for this changed layout?
 
Upvote 0
Looks like the layout of the data has changed since the beginning when it went from column A to column N.
Now it goes to column P. :)

Can you confirm which columns need to be added for this changed layout?
it is true sir, it has changed and sorry for that I have deleted some columns on my first attachement I sent you.. ;) The columns are J, K and P.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

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