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!
 
Holy crap I think I figured it out!

I just ran the below. Please tell me it makes sense?!

Code:
Option Compare Text
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "K").End(xlUp).Row
 For r = lr To 2 Step -1
   If InStr(Range("K" & r).Value, "AUD") And (Range("L" & r) = 0) Then Rows(r - 1 & ":" & r + 1).Delete
 Next r
End Sub
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Well done, minor tweak.....you don't need the brackets round the 2nd range
Code:
(Range("L" & r) = 0)

can be 

Range("L" & r) = 0
 
Upvote 0
balanceshift

I understand that you have a working result to the original question. However, you said you had a huge sheet and if you have to do this task again, you might consider these approaches that are considerably faster. I'm assuming that your subtotals have been applied using Excel's built-in Subtotal feature.

Now it does depend on what you mean by "huge" and how many subtotal rows there are, but with my sample data of 200,000 rows my method 1 below is roughly 20 times faster than what you have used and method 2 is about 15 times faster. The speed increase is due to not having to check every row to see if it is a subtotal row but going directly to those rows.

So ..

Method 1
This method requires that one of the columns you are subtotaling (NOT Column B) contains no formulas, just data, other than the formulas in the subtotal rows.
In my example, that is column C. Change that if needed to suit your data.

As it stands, this code also inserts 2 blank rows before the Grand Total row. If that is a problem, an extra line in the code can fix it.
Code:
Sub InsertRows1()
  Columns("C").SpecialCells(xlFormulas).Offset(1).EntireRow.Insert
End Sub


Method 2
If method 1 is unsuitable (or just for interest) you could try this
Code:
Sub InsertRows2()
  Dim lr As Long
  Dim cell As Range

  Application.ScreenUpdating = False
  lr = Range("B" & Rows.Count).End(xlUp).Row - 1
  ActiveSheet.Outline.ShowLevels RowLevels:=2
  For Each cell In Range("B2", Range("B" & lr)).SpecialCells(xlVisible)
    cell.Offset(1).EntireRow.Insert
  Next cell
  ActiveSheet.Outline.ShowLevels RowLevels:=3
  Application.ScreenUpdating = True
End Sub

BTW, about how many rows/columns of data are you dealing with?

And which version of Excel are you using?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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