Macro Help?

balanceshift

New Member
Joined
Feb 27, 2013
Messages
14
Hi Guys,

First post here! I have no idea what I'm doing with this forum or Exel, so apologies in advance!

I'm hoping you all can help me. I have some basic programming knowledge but not enough to be able to write a macro in Exel off the top of my head.

My boss at work has asked me to add a line break after every subtotal in a huge spreadsheet, so I'm hoping to have a macro:
- Search for the string "Total" in column B
- Add a line break below it (move down a cell, shift+space, ctrl+'+' ?)

Any assistance would be super muchly appreciated!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi and welcome to the Board
Try
Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
    For r = lr To 2 Step -1
    If Range("B" & r).Value = "Total" Then
    Rows(r + 1).Insert
    End If
Next r
End Sub
 
Upvote 0
balanceshift,

I tried running it as well. I found that if your "subtotal" line is not exactly "Total" then the macro won't work. I had to delete the text before "total" for it to work.
 
Last edited:
Upvote 0
Ok, try this.
This code will insert a row if the word "Total" is in the cell and also accounts for Upper case, lower case, etc
If this isn't what you require you will need to give more exact information
Code:
Option Compare Text
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
    For r = lr To 2 Step -1
    If InStr(Range("B" & r).Value, "Total") Then
    Rows(r + 1).Insert
    End If
Next r
End Sub

Also, here are the instructions for "Installing" the code, courtesy of Hiker95


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
 
Upvote 0
This'll get rid of the flashing
Code:
Option Compare Text
Sub MM1()
Dim lr As Long, r As Long
application.screenupdating=False
lr = Cells(Rows.Count, "B").End(xlUp).Row
    For r = lr To 2 Step -1
    If InStr(Range("B" & r).Value, "Total") Then
    Rows(r + 1).Insert
    End If
Next r
application.screenupdating=True
End Sub
 
Upvote 0
Thanks. I don't mind - I'm just going to let it run for a while.

I'm guessing that it's going to stop once it's gone through all the data? Does "lr = Cells(Rows.Count, "B").End(xlUp).Row" limit it? :)

Thanks again for all the help!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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