VBA code to set page headers in a special way

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
My table is like this:

Range(“A3:J” & lr)

Where lr is the last used row in column B

So, column B contain names which has associated categories in column C.

At the end of each category in column C, there is a “SUB-TOTAL” string in column B.

Now here is what I want to do:
When I get to a “SUB-TOTAL” string in column B, I want to have three (3) blank rows inserted below the “SUB-TOTAL” string.

Then after that I want to copy the contents of the range A1:J2 and paste it at the second and third rows of the the blank rows I have just inserted(that is to create a header for the next category)

*****The first blank row is there to separate one category from another******

Then I repeat the copy and pasting of A1:J2 for each SUB-TOTAL that I come across in the column B.

FYI:
The Range A1:J1 is merged.

I also want to apply the row heights of the source to the destination.

I am using this approach because I want to have multiple categories on a sheet when I print out and still have my headers for each category.

Can someone please help me out with the code for that?

Thanks in advance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
The following uses the Find method to locate only the cells with the text "Sub-total", that makes it faster than going row by row.

VBA Code:
Sub InsertHeader()
  Dim r As Range, f As Range, cell As String
  Application.ScreenUpdating = False
  
  Set f = Range("B:B").Find("SUB-TOTAL", , xlValues, xlWhole, xlByRows, xlPrevious, False)
  If Not f Is Nothing Then
    Set r = Range("B3:B" & f.Row - 1)
    Set f = r.Find("SUB-TOTAL", , xlValues, xlWhole, xlByRows, xlNext, False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        f.Offset(1).Resize(3).EntireRow.Insert
        Range("A1:J2").Copy f.Offset(2, -1)
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  End If
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
@DanteAmor
It worked so great.

One or two touches to the code and all be great:
1. I want to remove the background fill from the empty row.
2. I remove all vertical borders on the empty row (so they appear separated from each other)
 
Upvote 0
Add this line:
VBA Code:
f.Offset(1).Resize(3).EntireRow.Clear


Rich (BB code):
Sub InsertHeader()
  Dim r As Range, f As Range, cell As String
  Application.ScreenUpdating = False
  
  Set f = Range("B:B").Find("SUB-TOTAL", , xlValues, xlWhole, xlByRows, xlPrevious, False)
  If Not f Is Nothing Then
    Set r = Range("B3:B" & f.Row - 1)
    Set f = r.Find("SUB-TOTAL", , xlValues, xlWhole, xlByRows, xlNext, False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        f.Offset(1).Resize(3).EntireRow.Insert
        f.Offset(1).Resize(3).EntireRow.Clear
        Range("A1:J2").Copy f.Offset(2, -1)
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
There it goes
VBA Code:
Sub InsertHeader()
  Dim r As Range, f As Range, cell As String
  Application.ScreenUpdating = False
  
  Set f = Range("B:B").Find("SUB-TOTAL", , xlValues, xlWhole, xlByRows, xlPrevious, False)
  If Not f Is Nothing Then
    Set r = Range("B3:B" & f.Row - 1)
    Set f = r.Find("SUB-TOTAL", , xlValues, xlWhole, xlByRows, xlNext, False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        f.Offset(1).Resize(3).EntireRow.Insert
        f.Offset(1).Resize(3).EntireRow.Clear
        Range("A1:J2").Copy f.Offset(2, -1)
        f.Offset(2).RowHeight = Range("A1").RowHeight
        f.Offset(3).RowHeight = Range("A2").RowHeight
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 1

Forum statistics

Threads
1,215,868
Messages
6,127,413
Members
449,382
Latest member
DonnaRisso

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