Add blank row only applying to active worksheet

Jaye16

New Member
Joined
Apr 13, 2023
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hi all,

I am very new to Excel but have been trying to Frankenstein together a macro to knock out a number of tasks.

The macro runs right now (it's not pretty) but one portion of the code is only running on whatever worksheet is active (or at least I'm assuming. It doesn't work if I'm not on the WS I want it to run on, but if I'm on that sheet, it does.)

VBA Code:
Sub SetSheetProperties()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        'Set all sheets to legal size
        ws.PageSetup.PaperSize = xlPaperLegal
 
        'For sheets titled "Order history", wrap text and shrink columns wider than 100 to 90
        If ws.Name = "Order history" Then
            Dim col As Range
            For Each col In ws.UsedRange.Columns
                If col.ColumnWidth > 100 Then
                    col.WrapText = True
                    col.ShrinkToFit = True
                    col.ColumnWidth = 90
                End If
            Next col
        End If
 
        'For sheets titled "Subscription info", bold specific cells and insert blank rows
        If ws.Name = "Subscription info" Then
            Dim findValue As String
            Dim foundCell As Range
 
            'Bold cells with specific text
            findValue = "Account Information"
            Set foundCell = ws.Cells.Find(findValue)
            Do Until foundCell Is Nothing
                foundCell.Font.Bold = True
                Set foundCell = ws.Cells.FindNext(foundCell)
                If foundCell.Address = ws.Cells.Find(findValue).Address Then Exit Do
            Loop
 
            findValue = "Payment Information"
            Set foundCell = ws.Cells.Find(findValue)
            Do Until foundCell Is Nothing
                foundCell.Font.Bold = True
                Set foundCell = ws.Cells.FindNext(foundCell)
                If foundCell.Address = ws.Cells.Find(findValue).Address Then Exit Do
            Loop
 
            findValue = "Address History"
            Set foundCell = ws.Cells.Find(findValue)
            Do Until foundCell Is Nothing
                foundCell.Font.Bold = True
                Set foundCell = ws.Cells.FindNext(foundCell)
                If foundCell.Address = ws.Cells.Find(findValue).Address Then Exit Do
            Loop
 
            'Insert blank rows above cells with specific text
            Dim r As Long
            Dim lastRow As Long
            lastRow = Range("A" & Rows.Count).End(xlUp).Row
 
            For r = lastRow To 1 Step -1
                If Cells(r, 1).Value = "Account Information" And Cells(r, 1).Row > 1 Then Rows(r).Insert
            Next r
            
            'Set the Subscription info sheet to short date
            ws.Cells.NumberFormat = "mm/dd/yyyy"
            
            'Delete rows that only have data in column E
            Dim i As Long
            lastRow = ws.Range("E" & Rows.Count).End(xlUp).Row
            
            For i = lastRow To 1 Step -1
                If ws.Range("E" & i).Value <> "" And ws.Range("A" & i).Value = "" And ws.Range("B" & i).Value = "" And ws.Range("C" & i).Value = "" And ws.Range("D" & i).Value = "" And ws.Range("F" & i).Value = "" Then
                    ws.Range("E" & i).EntireRow.Delete
                End If
            Next i
        End If
    Next ws
End Sub

The portion I'm having trouble with is toward the bottom----

VBA Code:
            'Insert blank rows above cells with specific text
            Dim r As Long
            Dim lastRow As Long
            lastRow = Range("A" & Rows.Count).End(xlUp).Row
 
            For r = lastRow To 1 Step -1
                If Cells(r, 1).Value = "Account Information" And Cells(r, 1).Row > 1 Then Rows(r).Insert
            Next r

When I run this macro, everything runs where it is supposed to regardless of the active sheet---except for this bit. For this bit, it does not apply unless I am on the sheet I want it run on "Subscription info"

I'm also trying to get it so that Header&Footer display on all sheets. The header and footer already have data in them that I want to leave, I simply want it to display the header and footer whereas the native file does not unless I click the Header/Footer button.

Can anyone help me clean this up and find out why it won't run how I'd like?

Thank you!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Don't you just need
VBA Code:
'Insert blank rows above cells with specific text
            Dim r As Long
            Dim lastRow As Long
            lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
 
            For r = lastRow To 1 Step -1
                If ws.Cells(r, 1).Value = "Account Information" And ws.Cells(r, 1).Row > 1 Then ws.Rows(r).Insert
            Next r
 
Upvote 0
Don't you just need
VBA Code:
'Insert blank rows above cells with specific text
            Dim r As Long
            Dim lastRow As Long
            lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
 
            For r = lastRow To 1 Step -1
                If ws.Cells(r, 1).Value = "Account Information" And ws.Cells(r, 1).Row > 1 Then ws.Rows(r).Insert
            Next r

Yes! Thank you so much. That was simple!
Are you able to help with also trying to get it so that Header&Footer display on all sheets. The header and footer already have data in them that I want to leave, I simply want it to display the header and footer whereas the native file does not unless I click the Header/Footer button?

Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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