Inserting the same header to multiple rows of a spreadsheet

Rick G

Board Regular
Joined
Sep 11, 2003
Messages
62
Hi:

I'm currently running Excel version 2003.

My spreadsheet has approx. 15,000 rows and 15 columns with corresponding headings.
One of the column headings is used to identify the catalog page number of an item. Many items appear on the same catalog page #. Not every page number contains the same amount of items. All of the page numbers are sorted in ascending order.

I would like insert a new row into the spreadsheet for every change in page number and automatically copy the same headings from row 1 into each newly added row of the spreadsheet.

I have little to no knowledge on how to write this formula and the catalog contains 2144 pages. Can you help me please?

I really don't have time to manually perform this task.

Thank you.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Here's a macro that you can try on a copy of your workbook. I assumed that your first heading is in cell A1 and the page # heading is in column C (column number 3). Change these to fit your situation.
Code:
Sub InsertHeaders()
Dim rng As Range, pgColNum As Long, lRw As Long

pgColNum = 3  'Change this to the number of the column that has page # heading
lRw = Cells(Rows.Count, pgColNum).End(xlUp).Row
Set rng = Range(Cells(2, 1), Cells(lRw, 15))
For i = lRw - 1 To 2 Step -1
    If Cells(i, pgColNum).Value <> Cells(i, pgColNum).Offset(1, 0).Value Then
        Cells(i, pgColNum).Offset(1, 0).EntireRow.Insert
        rng.Rows(i).Value = rng.Rows(1).Offset(-1, 0).Value
    End If
Next i
End Sub
 
Upvote 0
Fantastic! It works beautifully (in about 3 seconds!!!!).

One item I failed to mention in my original posting (and is not a big deal) is that when the header row is copied automatically at each page # change via the macro, I was wondering if it would be possible to have all newly created "header rows" shaded in a light grey color.

Can this be done without too much difficulty? If not, it can be done manually with little to no effort. I don't want to bother you unnecessarily and really appreciate the help you have provided me with the code you wrote below.

Thank you so much!

Rick
 
Upvote 0
This should fill the new headers with light grey and run a bit faster too. Don't forget to alter the pgColNum to fit your data.
Code:
Sub InsertHeaders()
Dim rng As Range, pgColNum As Long, lRw As Long

Application.ScreenUpdating = False
pgColNum = 3  'Change this to the number of the column that has page # heading
lRw = Cells(Rows.Count, pgColNum).End(xlUp).Row
Set rng = Range(Cells(2, 1), Cells(lRw, 15))
For i = lRw - 1 To 2 Step -1
    If Cells(i, pgColNum).Value <> Cells(i, pgColNum).Offset(1, 0).Value Then
        Cells(i, pgColNum).Offset(1, 0).EntireRow.Insert
        With rng.Rows(i)
            .Value = rng.Rows(1).Offset(-1, 0).Value
            .Interior.Color = RGB(220, 220, 220)  'Increase the 220's upward to lighten fill
        End With
    End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

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