VBA Program needed to insert page breaks and insert header when value in row changes

dinesababu1

New Member
Joined
Oct 28, 2016
Messages
13
Dear Excel Community

I really need your help! I have a ton of bills which I need to submit for payment and I need help to get the data into the format I require.

Basically I have a spreadsheet with list of company codes in Column A. I need a macro that can insert a particular selection when the value in column A changes.

I have no issue copying the selection, but when I try to insert the selection the macro does not work.

The following is what I am trying, but it isn't working, can someone please help?
Sheets("Formula").Select
Rows("7:14").Select
Selection.Copy
Sheets("Final Format").Select

Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row To 9 Step -1
If Cells(lRow, "A") <> Cells(lRow - 1, "A") Then Selection.Insert
Next lRow
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Does this macro do what you want...
Code:
Sub InsertIntermediateHeaders()
  Dim R As Long
  For R = Cells(Rows.Count, "A").End(xlUp).Row To 9 Step -1
    If Cells(R, "A").Value <> Cells(R - 1, "A").Value Then
      Rows(R).Resize(8).Insert
      Sheets("Formula").Rows("7:14").Copy Cells(R, "A")
    End If
  Next
End Sub
 
Upvote 0
Rick, You are AWESOME!

The coding worked!

I could use a little more help if you do not mind....

I could use a coding that inserts a page break right above the selection we just inserted ...

Furthermore, Though I need a header every time the value changes... I also need to insert a selection every 40 rows if the value stays the same. So lets say column A stays the same for 60 rows, I need to insert the selection right after the 40th row... (This way so when I print no page will be too crowded.)

Do you think you can add additional coding to help me with this?
 
Upvote 0

Forum statistics

Threads
1,215,652
Messages
6,126,037
Members
449,281
Latest member
redwine77

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