VBA code to reduce total number of rows

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
I have a excel sheet with around 5lakh rows and i want to reduce number of rows by cumilating the data so that i can add more.
below is the sample data (Couldn't share original data due to privacy reasons). Data should be combined based on Columns B,D,E,G.

If data present in B,D,E,F columns is same the code has to sum the values in G column and make it in to single row of data as shown in the next table.

ABCDEFG
City CodeCity NamePincodeDateTypeDescriptionTotal
1NewYork321-Jan-20SAon40
1NewYork321-Jan-20SAon40
1NewYork321-Jan-20NAoff20
2Sydney4420-Jan-20NAoff40
2Sydney4420-Jan-20NAoff40
3Delhi9420-Jan-20SAon20
3Delhi9420-Jan-20SAon20
3Delhi9420-Jan-20SAon40

Out put expected is below.

City CodeCity NamePincodeDateTypeDescriptionTotal
1NewYork321-Jan-20SAon80
1NewYork321-Jan-20NAoff20
2Sydney4420-Jan-20NAoff80
3Delhi9420-Jan-20SAon80
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try....as long as the data is sorted

VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Cells(r, 2) = Cells(r - 1, 2) And Cells(r, 4) = Cells(r - 1, 4) And Cells(r, 5) = Cells(r - 1, 5) And Cells(r, 6) = Cells(r - 1, 6) Then
        Cells(r - 1, 7) = Cells(r, 7) + Cells(r - 1, 7)
        Rows(r).Delete
    End If
Next r
End Sub
 
Upvote 0
Hi M
Try....as long as the data is sorted

VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Cells(r, 2) = Cells(r - 1, 2) And Cells(r, 4) = Cells(r - 1, 4) And Cells(r, 5) = Cells(r - 1, 5) And Cells(r, 6) = Cells(r - 1, 6) Then
        Cells(r - 1, 7) = Cells(r, 7) + Cells(r - 1, 7)
        Rows(r).Delete
    End If
Next r
End Sub
Hi Michael,

Its taking more time as there are aroung 500k lines. Can you help me with a code that gives result in to a new sheet leaving the source as it is.
Consider workbook name is "wk" and source is in the sheet "Source".
 
Upvote 0
Try

VBA Code:
Sub MM1()
Dim lr As Long, r As Long, sh As Worksheet
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "A").End(xlUp).Row
Sheets.Add
ActiveSheet.Name = "NewSheet"
Set sh  = Sheets("NewSheet")
Sheets("Source").UsedRange.Copy sh.Range("A1")
With sh
For r = lr To 2 Step -1
    If Cells(r, 2) = Cells(r - 1, 2) And Cells(r, 4) = Cells(r - 1, 4) And Cells(r, 5) = Cells(r - 1, 5) And Cells(r, 6) = Cells(r - 1, 6) Then
        Cells(r - 1, 7) = Cells(r, 7) + Cells(r - 1, 7)
        Rows(r).Delete
    End If
Next r
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try

VBA Code:
Sub MM1()
Dim lr As Long, r As Long, sh As Worksheet
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "A").End(xlUp).Row
Sheets.Add
ActiveSheet.Name = "NewSheet"
Set sh  = Sheets("NewSheet")
Sheets("Source").UsedRange.Copy sh.Range("A1")
With sh
For r = lr To 2 Step -1
    If Cells(r, 2) = Cells(r - 1, 2) And Cells(r, 4) = Cells(r - 1, 4) And Cells(r, 5) = Cells(r - 1, 5) And Cells(r, 6) = Cells(r - 1, 6) Then
        Cells(r - 1, 7) = Cells(r, 7) + Cells(r - 1, 7)
        Rows(r).Delete
    End If
Next r
End With
Application.ScreenUpdating = True
End Sub
It's taking hours to finish the process mate. Is they any other way to do this?(data might is sorted this time but i might not get sorted data everytime)
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
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