Macro Help

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
Hello,

I have a macro that needs some tweaking and help.
Currently it take data from the first tab, breaks it out into new tabs based on data in column A. Then within the new tabs, it formats each sheet by adding "total" rows after a new value in column B. The new row adds an auto sum to columns J and L and a grand total at the bottom using the sums from column L.

I need to have column I do the same thing as column B in the new row created, but then remove the contents in the cells above the new row. So the information is only in the new row created in column I.

Please see before/after pictures.
Before:
2v2iddy.jpg

After:
288m04.jpg


I have also included my code below -

Code:
 'adds grey total row after invoice number and grand total
   For i = Range("B" & Rows.Count).End(xlUp).Row + 1 To 3 Step -1
      If Range("B" & i).Value <> Range("B" & i - 1).Value Then
            Range("B" & i).EntireRow.Insert
            Range("B" & i).Offset(0, -1).Resize(, 12).Interior.Color = 14277081
            Range("B" & i).Offset(, 1).Value = "Total"
            Range("B" & i).Offset(, 1).Font.Bold = True
      End If
   
   Next i
   TotRw = Range("L" & Rows.Count).End(xlUp).Offset(3).Row
   For Each Rng In Range("L2:L" & TotRw - 1).SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count, -10).Resize(1)
         .Value = Rng.Offset(, -10).Value
         .Font.Bold = True
      End With
      
      Rng.Offset(Rng.Count).Resize(1).Formula = "=sum(" & Rng.Address & ")"
      Rng.Offset(Rng.Count).Resize(1).Font.Bold = True
   
   Next Rng
   With Range("L" & TotRw)
      .Formula = "=sum(" & Range("H2:H" & TotRw - 2).SpecialCells(xlBlanks).Offset(, 4).Address & ")"
      .Offset(, -1).Value = "Grand Total"
      .Offset(, -1).Resize(, 2).Font.Bold = True
      .Borders(xlEdgeTop).LineStyle = xlContinuous
      .Borders(xlEdgeBottom).LineStyle = xlDouble
   End With
    
   'adds sub totals in column J
   For Each Rng In Range("J2:J" & TotRw - 1).SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count, -8).Resize(1)
         .Value = Rng.Offset(, -8).Value
         .Font.Bold = True
      End With
      
      Rng.Offset(Rng.Count).Resize(1).Formula = "=sum(" & Rng.Address & ")"
      Rng.Offset(Rng.Count).Resize(1).Font.Bold = True
   
   Next Rng


Thanks, I appreciate any help!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
Code:
For Each rng In Range("L2:L" & TotRw - 1).SpecialCells(xlConstants).Areas
      With rng.Offset(rng.Count, -10).Resize(1)
         .Value = rng.Offset(, -10).Value
         .Font.Bold = True
      End With
      With rng.Offset(rng.Count, -3).Resize(1, 1)
         .Value = rng.Offset(, -3).Value
         .Font.Bold = True
      End With
      rng.Offset(, -3).ClearContents
 
Upvote 0
Thanks! This worked perfect :)


I'm trying to figure out how to write a code to enable the worksheet protection. Can you also help with that?
 
Upvote 0
Depends on what protection you want.
The easiest way, is to record a macro as you select what protection you want. Then you can add that to the code.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,681
Members
449,116
Latest member
HypnoFant

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