+ Reply to Thread
Results 1 to 21 of 21

macro to print selected sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    8

    macro to print selected sheets

    hey, I need a help in creating a macro so that I can print from sheet 1 to sheet name "XYZ" as an array. In other words I want to print selected sheets as one command so that page number in the footer will automatically change.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro to print selected sheets

    Hello HardWorker,

    Welcome to the Forum!

    Here is macro that will loop through all the worksheets in the workbook selecting only the ones you want. The macro allows you to skip sheets. Currently the only sheet skipped is "Sheet1". You add the names of the sheets you don't selected to the code. Place the name in double quotes and separate the names with a comma.
    Sub PrintSheets()
    
      Dim I As Long
      Dim Shts() As String
      
        For Each Sht In Sheets
          Select Case Sht.Name
            Case Is = "Sheet1"
              'Do Nothing - Skip these worksheets
            Case Else
              ReDim Preserve Shts(I)
              Shts(I) = Sht.Name
              I = I + 1
          End Select
        Next Sht
        
        Sheets(Shts).Select
        Sheets.PrintOut
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-16-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: macro to print selected sheets

    Hey thanks,
    What if I don't want to skip any sheets between my section. For example if I have 30 sheets in my excel file and I want to print consequently from sheet 1 to sheet 25.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro to print selected sheets

    Hello hard worker,

    This code will print sheets 1 to 25...
    Sub PrintSheets()
    
      Dim I As Long
      Dim Shts() As String
      
        For I = 1 to 25
          ReDim Preserve Shts(I)
          Shts(I - 1) = Sht.Name
        Next Sht
        
        Sheets(Shts).Select
        Sheets.PrintOut
        
    End Sub

  5. #5
    Registered User
    Join Date
    03-16-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: macro to print selected sheets

    Both macro are not working. The one which suppose to skip "sheet 1" is not skipping that sheet and printing all sheets. And the one which suppose to print pages 1 to 25 is giving a error. It says "invalid next control variable reference".

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro to print selected sheets

    Hello hardworker,

    Here is the corrected code for the first 25 sheets. I forgot the index variable for the sheet name.
    Sub PrintSheets()
    
      Dim I As Long
      Dim Shts() As String
      
        For I = 1 to 25
          ReDim Preserve Shts(I)
          Shts(I - 1) = Shts(I).Name
        Next Sht
        
        Sheets(Shts).Select
        Sheets.PrintOut
        
    End Sub

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro to print selected sheets

    eahmed,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro to print selected sheets

    KEMelton,

    As i said to eahmed above,

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1