Results 1 to 11 of 11

Thread: [RESOLVED] How can I border each page in excel when page length could vary?

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Resolved [RESOLVED] How can I border each page in excel when page length could vary?

    This sounded so easy when I sat to do it but everything I try seems to have some small but fundamental flaw.

    We produce a report in excel that has some general header stuff at the top, then a row of column headers, then a load of rows of output. The header stuff and column headers I've bordered appropriately and set as the .PrintTitleRows which works nicely. I now want to place a border around the output on each page (nb. around the whole page, not the individual cells) and the problem is getting the bottom border.

    To pre-empt some suggestion that don't work and maybe prompt some ideas in others, here's what I've tried/thought about so far:-

    1. Outputting a fixed number of rows per page then inserting page breaks - didn't work because some of the columns need to wrap text so I can't guarantee that all the rows will fit on a page.
    2. Putting a blank line with a top border at the bottom of the document and setting it as a footer - didn't work because the last page might end part way down a page and I want the border to go round the bottom row ratherthan the bottom of the page in that case.
    3. Going through each row in turn to what print page it would be on and, if it's different to the last row stick in a border and page break - I think there might be something in that but I can't find a property that will tell me which page a row would print on.
    4. Similar to 1 but turn wraping off and manually wrap the text down to the next row if it's too long - haven't tried this yet but how would I know the tex was too long - a string of 10 w's is longer than a string of 10 i's. Can't tell it to shrink to fit becuase the customer wantes all the text the same size.

    Can anyone suggest a way round this?
    Last edited by FunkyDexter; Mar 23rd, 2006 at 04:45 AM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How can I border each page in excel when page length could vary?

    Funky

    This can be done fairly easily using the HPageBreaks collection of the sheet in question. Here's some sample code to get you started.
    VB Code:
    1. Sub FunkyPageBreaks()
    2. Dim FunkyBreak As HPageBreak
    3.    
    4.     For Each FunkyBreak In Worksheets(1).HPageBreaks
    5.         With FunkyBreak.Location.Offset(-1, 0).EntireRow
    6.             .Borders(xlEdgeBottom).LineStyle = xlContinuous
    7.         End With
    8.     Next FunkyBreak
    9. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I border each page in excel when page length could vary?

    Now that looks exactly what I'm after. And the opportunity to write a function called FunkyPageBreaks is just too good to pass up

  4. #4

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I border each page in excel when page length could vary?

    I'm getting some funny results from that.

    1. If I use a for each loop it throws a subscript out of range error
    2. If I change that to a For next loop it throws a subscript out range error on element 0 (so presumably the collection has a base of 1) and on element hPageBreaks.count so I can't seem to get the last page break on.
    3. The report is spread across multiple sheets. If a subsequent sheet has more pages than the first it seems to throw the subscript out of range error on any page break that's higher than the number on the first sheet - I might be missunderstanding what's happening there though.

    Any ideas what's causing these subscript errors?

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How can I border each page in excel when page length could vary?

    Yes, you will get a subscript error using a FOR EACH.. loop if the PrintArea property of the PageSetup object does not have a value.
    To correct it you will need to set the PrintArea before adding the borders.

    Here's a code snippet where I'm setting the PrintArea to the UsedRange (wksSheet is an object variable that contains a worksheet.
    VB Code:
    1. wksSheet.PageSetup.PrintArea = wksSheet.UsedRange.Address
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How can I border each page in excel when page length could vary?

    Here's some more sample code. This procedure adds exterior borders to all the pages for a given worksheet in a given workbook. I haven't added error handling for invalid book or sheet names, but the rest of the code works.
    VB Code:
    1. Sub PageBreakBorder(BookName As String, SheetName As String)
    2. Dim wksSheet As Worksheet
    3. Dim rngPrint As Range
    4. Dim lRowOffset As Long
    5. Dim lColOffset As Long
    6. Dim HBreak As HPageBreak
    7. Dim VBreak As VPageBreak
    8. Dim rngStartCell As Range
    9.    
    10.     Set wksSheet = Workbooks(BookName).Worksheets(SheetName)
    11.    
    12.     Set rngPrint = wksSheet.UsedRange
    13.     lRowOffset = rngPrint.Rows.Count - 1
    14.     lColOffset = rngPrint.Columns.Count - 1
    15.    
    16.     'Set the print area
    17.     wksSheet.PageSetup.PrintArea = rngPrint.Address
    18.    
    19.     'Remove all existing borders
    20.     rngPrint.Borders.LineStyle = xlLineStyleNone
    21.    
    22.     'Add Outer Borders
    23.     rngPrint.Borders(xlEdgeTop).LineStyle = xlContinuous
    24.     rngPrint.Borders(xlEdgeBottom).LineStyle = xlContinuous
    25.     rngPrint.Borders(xlEdgeLeft).LineStyle = xlContinuous
    26.     rngPrint.Borders(xlEdgeRight).LineStyle = xlContinuous
    27.    
    28.     'Add Horizontal Borders
    29.     For Each HBreak In wksSheet.HPageBreaks
    30.         Set rngStartCell = HBreak.Location
    31.         'Add Top borders
    32.         Range(rngStartCell, rngStartCell.Offset(0, lColOffset)).Borders(xlEdgeTop).LineStyle = xlContinuous
    33.         'Add Bottom borders
    34.         Range(rngStartCell.Offset(-1, 0), rngStartCell.Offset(-1, lColOffset)).Borders(xlEdgeBottom).LineStyle = xlContinuous
    35.     Next HBreak
    36.    
    37.     'Add Vertical Borders
    38.     For Each VBreak In wksSheet.VPageBreaks
    39.         Set rngStartCell = VBreak.Location
    40.         'Add Left Borders
    41.         Range(rngStartCell, rngStartCell.Offset(lRowOffset, 0)).Borders(xlEdgeLeft).LineStyle = xlContinuous
    42.         'Add Right Borders
    43.         Range(rngStartCell.Offset(0, -1), rngStartCell.Offset(lRowOffset, -1)).Borders(xlEdgeRight).LineStyle = xlContinuous
    44.     Next VBreak
    45.    
    46.     'Clean up Object variables
    47.     Set rngStartCell = Nothing
    48.     Set rngPrint = Nothing
    49.     Set wksSheet = Nothing
    50. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I border each page in excel when page length could vary?

    I still can't seem to get it quite right I'm afraid. I've set the print area as suggested and I'm no longer getting odd subscript out of range errors but it seems to be finding 1 too few breaks if there's more than 1 break in the document - so if there's only 1 it's fine but if there's 3 it only finds 2. It borders the breaks it finds perfectly but just misses the last one altogether.

    Here's my code:-
    VB Code:
    1. Private Sub pcFormatSheet()
    2.  
    3.     Dim pgePageBreak As HPageBreak
    4.  
    5.     With mExcelSheet
    6.         'format the page
    7.         With .PageSetup
    8.             .Orientation = xlLandscape
    9.             .Zoom = False
    10.             .FitToPagesWide = 1
    11.             .FitToPagesTall = False
    12.         End With
    13.         'now put the bottom border on each page in turn
    14.         .PageSetup.PrintArea = .Range(.Cells(1, mclngCustName), .Cells(mlngEndOutput, mclngReRun + 1)).Address
    15.         For Each pgePageBreak In .HPageBreaks
    16.             pgePageBreak.Location.Offset(-1, 0).EntireRow.Borders(xlEdgeBottom).Weight = xlThin
    17.             pgePageBreak.Location.Offset(-1, 0).EntireRow.Borders(xlEdgeBottom).LineStyle = xlContinuous
    18.         Next pgePageBreak
    19.     End WithEnd Sub

    I'm using a variable mlngEndOutput to set where the range needs to end rather than the usedRange property but I've checked it's value as it comes through and it's what I'd expect (I've also tried it with usedRange and got the same results).

    It occurred to me it might be something to do with being in landscape rather than portrait but I don't think so because the breaks it sets are in the right place. Is it possible that using fitToPagesTall is somehow throwing it?

    Any ideas?

    >>Just had another thought. I'm setting Rows 1 to 9 as PrintTitleRows - could that be throwing it off?
    Last edited by FunkyDexter; Mar 27th, 2006 at 06:53 AM.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How can I border each page in excel when page length could vary?

    Funky
    I think the issue you are having is around what page breaks are. Page breaks only exist between pages. The last page will have a break before but not a break afterwards. You will always need to add a border to the bottom of the report, independantly of the page break borders.

    This is why, in my example code I added borders to the outer edges of the report.
    VB Code:
    1. 'Add Outer Borders
    2.     rngPrint.Borders(xlEdgeTop).LineStyle = xlContinuous
    3.     rngPrint.Borders(xlEdgeBottom).LineStyle = xlContinuous
    4.     rngPrint.Borders(xlEdgeLeft).LineStyle = xlContinuous
    5.     rngPrint.Borders(xlEdgeRight).LineStyle = xlContinuous
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I border each page in excel when page length could vary?

    No I'm totally happy on what pagebreaks are. There's another lump of code immediately above this one that borders the entire table. I didn't post it before to save space but this:-
    VB Code:
    1. .Columns(mclngTestType).ColumnWidth = 13.43
    2.         .Columns(mclngTestNo).ColumnWidth = 6
    3.         .Columns(mclngCands).ColumnWidth = 8.86
    4.         .Columns(mclngDate).ColumnWidth = 8.43
    5.         .Columns(mclngReRun).ColumnWidth = 16.86
    6.         'border the table
    7.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeLeft).LineStyle = xlContinuous
    8.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeLeft).Weight = xlThin
    9.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeTop).LineStyle = xlContinuous
    10.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeTop).Weight = xlThin
    11.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeBottom).LineStyle = xlContinuous   'this bit puts the bottom edge in
    12.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeBottom).Weight = xlThin
    13.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeRight).LineStyle = xlContinuous
    14.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlEdgeRight).Weight = xlThin
    15.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlInsideVertical).LineStyle = xlContinuous
    16.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).Borders(xlInsideVertical).Weight = xlThin
    17.        
    18.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngStartOutput - 1, mclngReRun)).Borders(xlEdgeBottom).LineStyle = xlContinuous
    19.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngStartOutput - 1, mclngReRun)).Borders(xlEdgeBottom).Weight = xlThin
    20.        
    21.         'set text wrapping
    22.         .Range(.Cells(mlngStartOutput - 1, mclngCustName), .Cells(mlngEndOutput, mclngReRun)).WrapText = True
    all occurs immediately before the with .pageSetup line.

    It's really odd - If I've got a 4 page report I'd expect to find 3 page breaks - but it's only finding 2.

  10. #10

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I border each page in excel when page length could vary?

    Bizarre thing - I found the a post elsewhere on the net that said the active cell could throw off the count. I tried selecting the bottom right cell on my report and it now works absolutley fine.

    Thanks for your help with this Declan

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] How can I border each page in excel when page length could vary?

    The joys of excel

    Glad you got it sorted.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width