Macro to automatically sort a table

Jburgy

New Member
Joined
Aug 15, 2018
Messages
9
Hello!

I'm stuck trying to gaps into a table, I hope this makes sense

It starts off like this with around 10,000 rows and some redundant columns to delete but I need to (currently) go through and manually add a break in the table to add the order total and total size

I'm wondering if I can create a macro to do this quicker and where is the best place to start with figuring out how to do it. I tried recording a macro but it only did the task on one line before crashing. Any advice is greatly appreciated!! :)


RefCostSizeInfoDate
1234917.8Product 114/02/18
123410139.5Product 214/02/18
1235336.6Product 9719/04/18

<tbody>
</tbody>

Will need to turn into (Just image but with 400 Orders and 10,000 rows):
RefCostSizeInfoDate
1234917.8Product 114/02/18
123410139.5Product 214/02/18
TOTAL11057.3
1235336.6Product 9719/04/18

<tbody>
</tbody>


Is it possible to create a break every time the order reference changes and add in the total cells like above?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Sub addTotals()
   Dim i As Long
   Dim Rng As Range
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
   Next i
   For Each Rng In Range("A:A").SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count).Resize(1, 3)
         .Formula = Array("Totals", "=sum(" & Rng.Offset(, 1).Address & ")", "=sum(" & Rng.Offset(, 2).Address & ")")
         .Font.Bold = True
      End With
   Next Rng
End Sub
 
Upvote 0
How about
Code:
Sub addTotals()
   Dim i As Long
   Dim Rng As Range
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
   Next i
   For Each Rng In Range("A:A").SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count).Resize(1, 3)
         .Formula = Array("Totals", "=sum(" & Rng.Offset(, 1).Address & ")", "=sum(" & Rng.Offset(, 2).Address & ")")
         .Font.Bold = True
      End With
   Next Rng
End Sub

This is great thank you so much!

If I add more columns to total up say in column I, how would I then also total those with the formula above? I can't quite figure it out :(
 
Upvote 0
If you wanted to sum columns 2:4 add to the code like
Code:
Sub addTotals()
   Dim i As Long
   Dim Rng As Range
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
   Next i
   For Each Rng In Range("A:A").SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count).Resize(1, [COLOR=#ff0000]4[/COLOR])
         .Formula = Array("Totals", "=sum(" & Rng.Offset(, 1).Address & ")", "=sum(" & Rng.Offset(, 2).Address & ")", [COLOR=#ff0000]"=sum(" & Rng.Offset(, 3).Address & ")"[/COLOR])
         .Font.Bold = True
      End With
   Next Rng
End Sub
 
Upvote 0
Fantastic thank you, I did figure it out eventually but didn't know how to edit post to say so! (Didn't know if double post was allowed or no)
 
Upvote 0
Glad it's sorted & thanks for the feedback.

Posting more than once is fine, as long as you are not simply repeating yourself.
 
Upvote 0
Glad it's sorted & thanks for the feedback.

Posting more than once is fine, as long as you are not simply repeating yourself.

Thanks for letting me know!

If i want to also highlight the newly created row in grey for example, how would I do that?
 
Upvote 0
Like
Code:
Sub addTotals()
   Dim i As Long
   Dim Rng As Range
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
   Next i
   For Each Rng In Range("A:A").SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count).Resize(1, 4)
        [COLOR=#0000ff] .Interior.Color = 14277081[/COLOR]
         .Formula = Array("Totals", "=sum(" & Rng.Offset(, 1).Address & ")", "=sum(" & Rng.Offset(, 2).Address & ")", "=sum(" & Rng.Offset(, 3).Address & ")")
         .Font.Bold = True
      End With
   Next Rng
End Sub
 
Upvote 0
Like
Code:
Sub addTotals()
   Dim i As Long
   Dim Rng As Range
   
   For i = Range("[B]D[/B]" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Range("[B]D[/B]" & i).Value <> Range("[B]D[/B]" & i - 1).Value Then Rows(i).Insert
   Next i
   For Each Rng In Range("[B]D:D[/B]").SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count).Resize(1, 4)
        [COLOR=#0000ff] .Interior.Color = 14277081[/COLOR]
         .Formula = Array("Totals", "=sum(" & Rng.Offset(, 1).Address & ")", "=sum(" & Rng.Offset(, 2).Address & ")", "=sum(" & Rng.Offset(, 3).Address & ")")
         .Font.Bold = True
      End With
   Next Rng
End Sub

Thank you so much again!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,381
Messages
6,136,229
Members
450,000
Latest member
jgp19

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