Consulting

Results 1 to 7 of 7

Thread: Moving from sheet to sheet

  1. #1
    VBAX Regular
    Joined
    Mar 2005
    Posts
    6
    Location

    Question Moving from sheet to sheet

    Hiya,

    I am having trouble with the following coding. I want to format sheets in a workbook one after the other. The coding below will do it to only the active sheet and will not move from one to the next:

    Sub FormatExcelSheet()
    Dim WS As Worksheet
    For Each WS In ThisWorkbook.Worksheets ' do something with WS
    With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    etc...
    End With
    Next WS
    End Sub
    Can anyone help - it would be much appreciated??

    Ed.
    Last edited by Airborne; 03-11-2005 at 10:24 AM.

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Ed,

    Welcome to VBAX. Does this do what you want?

    Sub FormatExcelSheet()
    Dim WS As Worksheet
    ActiveSheet.Cells.Select
    Selection.Copy
    For Each WS In ThisWorkbook.Worksheets
    WS.Activate
    Range("A1").Select
    ActiveSheet.Paste
    Next WS
    End Sub
    (you have to activate each sheet)
    HTH

    John


    PS If you mean to do something other than copy and paste, just change the code where appropriate...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Regular
    Joined
    Mar 2005
    Posts
    6
    Location

    Question

    Hi John,

    Thanks but not quite (failing in my question & not your answer!). I want to adjust the page setup on each sheet and not just the format of each sheet.

    Sub FormatExcelSheet() 
    Dim WS As Worksheet
    For Each WS In ThisWorkbook.Worksheets 
    With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    etc...
    End With
    Next WS
    End Sub
    This adjusts the pagesetup of one sheet but not the next.

    Kind regards,

    Ed
    Last edited by Airborne; 03-11-2005 at 10:26 AM.

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Ed,

    You probably read my reply before I edited it... The main problem is you are saying

    For Each WS In ThisWorkbook.Worksheets 
    With ActiveSheet.PageSetup
    The "With ActiveSheet" you have always refers to your first sheet because you're not activating each new sheet after the "For Each WS...", you need to put "WS.Activate" as in the example I gave.

    PS To put your code as mine is, put (vba) before the code and (/vba) (but use square brackets) at the end of it - it makes it a lot easier to read

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try


    For Each sh In Sheets
        With sh.PageSetup
            .Orientation = xlLandscape
        End With
    Next

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Ed

    Try this

    Sub FormatExcelSheet()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets ' do something with WS
         With ws.PageSetup
              .PrintTitleRows = ""
              'etc...
         End With
    Next WS
    End Sub

  7. #7
    VBAX Regular
    Joined
    Mar 2005
    Posts
    6
    Location
    Thanks to all that helped with this query. The following code appears to work and moves from sheet to sheet to change the page setup:


    For Each sh In Sheets 
        With sh.PageSetup 
            .Orientation = xlLandscape 
        End With 
    Next


    Kind regards,

    Paul

Posting Permissions

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