PDA

View Full Version : After page break bottom border for all pages



elsuji
09-07-2019, 10:20 PM
Hi,

I am using the following code for applying border on all pages after page break



Sub Test()
Dim i1 As Long
Dim sh As Worksheet, Rg As Range, LastRow1 As Long, Count As Long
ii = 41 ' first page break
Set sh = Sheets("Sheet4")
With sh
LastRow1 = .Range("B" & .rows.Count).End(xlUp).Row
Count = LastRow1
Set Rg = .Range("B4", "G" & Count) 'The range of the document
If LastRow1 > 5 Then ' count is the number of row. Break at every 15 rows
.ResetAllPageBreaks
.PageSetup.PrintArea = Rg.Address
While Count > 4 And ii < LastRow1
If Count > 10 Then ' no page break if there is less than 15 rows left
'.Rows(ii).PageBreak = xlPageBreakManual
.HPageBreaks.Add Before:=.rows(ii)
End If
ii = ii + 35
Count = Count - 35
Wend
End If
End With

Dim i1 As Long
For i1 = 1 To ActiveSheet.HPageBreaks.Count
With Range(ActiveSheet.HPageBreaks(i1).Location.Address).Offset(-1).Resize(, 7).Borders(xlEdgeBottom)
.LineStyle = xlAutomatic
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Next i
End sub

But when i am running this code only one time it is running properly. Once i close the excel and again open it this code is not running. I am getting error message on " unable to set Linestyle property of the border class ".

Can any one help me to solve this issue