Results 1 to 4 of 4

Thread: Help needed in Excel

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Help needed in Excel

    Hi,

    I have a number of excel sheets that are not formatted and vary in the
    number of pages.

    I would like to have them landscaped and have a page break inserted after
    the “Page” number.

    The first page does not contain a “Page” number.

    Could you please provide me with a working sample of code to accomplish
    this task.

    Thank you in advance.
    Last edited by cssriraman; Apr 23rd, 2006 at 07:47 PM.
    CS

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Help needed in Excel

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Whatever()
    4. Dim shtSheet As Worksheet
    5.  
    6.     For Each shtSheet In Worksheets
    7.    
    8.         'Part A:
    9.         shtSheet.PageSetup.Orientation = xlLandscape
    10.         'Part B:
    11.         If shtSheet.Index <> 1 Then shtSheet.Rows(25).PageBreak = xlPageBreakManual
    12.    
    13.     Next
    14. End Sub

    I had a guess where the "Page Number" may be.... (Eg row 25) You can tweek as required.

  3. #3

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Help needed in Excel

    Here is the sample file uploaded. Please help.
    Attached Files Attached Files
    CS

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

    Re: Help needed in Excel

    Cssriraman

    I think I've got what you need. Rather than using the page number as the identifier, I used the title of the next page "ABC QUOTING CENTER" " as the placeholder to determine where to insert the page breaks. Here's the procedure that adds the breaks and a sample proc showing how its used.

    Let me know if this works.

    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Sub FormatReport(SheetName As String, TitleColumn As Long)
    5. Dim wksSheet As Worksheet
    6. Dim rngSearch As Range
    7. Dim lRowCount As Long
    8. Dim lRowNum As Long
    9.    
    10.     On Error Resume Next
    11.         Set wksSheet = ThisWorkbook.Worksheets(SheetName)
    12.     On Error GoTo 0
    13.    
    14.     'Trap for cases where the sheet nmae doesn't exist
    15.     If wksSheet Is Nothing Then Exit Sub
    16.            
    17.     With wksSheet
    18.         'Remove current breaks
    19.         .ResetAllPageBreaks
    20.  
    21.         With .PageSetup
    22.             'Set page as landscape and 1 page wide
    23.             .Orientation = xlLandscape
    24.             .Zoom = False
    25.             .FitToPagesWide = 1
    26.             .FitToPagesTall = False
    27.         End With
    28.        
    29.         'Column where we search for the page title
    30.         Set rngSearch = .Columns(TitleColumn)
    31.        
    32.         'Number of rows to check for page headers
    33.         lRowCount = .UsedRange.Rows.Count
    34.     End With
    35.    
    36.     'Ingnore the first page by starting at row 3
    37.     For lRowNum = 3 To lRowCount
    38.        
    39.         With rngSearch.Cells(lRowNum, 1)
    40.            
    41.             'When the title exist in the row...
    42.             If .Value = "ABC QUOTING CENTER " Then
    43.                
    44.                 '...insert a break one row up
    45.                 .Offset(-1, 0).PageBreak = xlPageBreakManual
    46.             End If
    47.            
    48.         End With
    49.        
    50.     Next lRowNum
    51.  
    52.     'Clear Object Variables
    53.     Set wksSheet = Nothing
    54. End Sub
    55.  
    56. 'Here's the code in action
    57. Sub sample()
    58.     FormatReport "Close Ratio by Customer", 8
    59.     FormatReport "Quotation Activity", 11
    60. End Sub
    Attached Files Attached Files
    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