Underline a worksheet

Fergus

Well-known Member
Joined
Mar 10, 2004
Messages
1,174
I have a very simple VBA sub which is intended to underline a worksheet at each page break when printed. The total length of the worksheet is 3574 rows. The sub works fine down to row number 1477 but then gives an error message "subscript out of range". I have used this in the past on Excel 2007 and it worked fine over worksheets of up to 600 rows.

Can anyone tell me what I am doing wrong? Here's the sub which I put into Thisworkbook. It is called from a button.

Sub underline()
For Each pgbr In ActiveSheet.HPageBreaks

With pgbr.Location
With Range(.Offset(-1, 0), .Offset(-1, 17)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
End With
Next
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I do not think you are doing anything wrong - I hit the same problem at the 20th page break :confused:

This seems to work for me with Excel 2016
Code:
Sub underline2()
    Dim pgBr As HPageBreak, c As Long
    For c = 1 To ActiveSheet.HPageBreaks.Count
        On Error Resume Next
        With ActiveSheet.HPageBreaks(c).Location
            With Range(.Offset(-1, 0), .Offset(-1, 17)).Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlThin
            End With
        End With
    Next
End Sub
 
Upvote 0
Thanks Yongle

Putting your code in gets down to page no 45 then ends without any error message. I haven't managed to get it down beyond that. When I try to debug and keep my finger on F8 c gets up to 72 but still the underline stops at 45.

Any ideas?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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