+ Reply to Thread
Results 1 to 6 of 6

Select a range of sheets.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Select a range of sheets.

    Hi,
    I am using the following code to create a table of contents but I don't want to use every sheet in the book. The ones at the back section are for data storage only.

    I also don't want to include the first data input page.

    Sub CreateTableOfContents()
        ' Determine if there is already a Table of Contents
    
        Dim WST As Worksheet
        On Error Resume Next
        Set WST = Worksheets("INDEX")
        If Not Err = 0 Then
            ' The Table of contents doesn't exist. Add it
            Set WST = Worksheets.Add(Before:=Worksheets(1))
            WST.Name = "INDEX"
        End If
        On Error GoTo 0
        
        ' Set up the table of contents page
        WST.[A15] = "CONTENTS"
    
      
        WST.[d15] = "PAGE"
        WST.Range("A1:B1").ColumnWidth = Array(36, 12)
        TOCRow = 17
        PageCount = -1
        ' Do a print preview on all sheets so Excel calcs page breaks
    
        Msg = "Excel needs to do a print preview to calculate the number of pages. "
        Msg = Msg & "Please dismiss the print preview by clicking close."
        MsgBox Msg
        ActiveWindow.SelectedSheets.PrintPreview
        ' Loop through each sheet, collecting TOC information
    ' Loop through each sheet, collecting TOC information
        For Each S In Worksheets
            If S.Visible = -1 Then
            S.Select
    ' Use any one of the following 3 lines
            ThisName = Range("A6").Value
            'ThisName = ActiveSheet.PageSetup.LeftHeader
            HPages = ActiveSheet.HPageBreaks.Count + 1
            VPages = ActiveSheet.VPageBreaks.Count + 1
            ThisPages = HPages * VPages
            ' Enter info about this sheet on TOC
            Sheets("INDEX").Select
            Range("A" & TOCRow).Value = ThisName
            Range("D" & TOCRow).NumberFormat = "@"
            If ThisPages = 1 Then
                Range("D" & TOCRow).Value = PageCount + 1 & " "
            Else
                Range("D" & TOCRow).Value = PageCount + 1
                '& " - " & PageCount + ThisPages
            End If
            PageCount = PageCount + ThisPages
            TOCRow = TOCRow + 2
            End If
        Next S
    End Sub
    Thanks
    Last edited by FDibbins; 07-28-2014 at 04:12 PM.

  2. #2
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: Select a range of sheets.

    It also seems to set Index page as 2 pages when there is only 1, not sure why.

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,052

    Re: Select a range of sheets.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    I will add them for you - this time
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: Select a range of sheets.

    Thank you.

  5. #5
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: Select a range of sheets.

    Can anyone help with this?

  6. #6
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: Select a range of sheets.


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to select a range of sheets in between two sheets using VBA
    By som3on3_10 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2013, 02:20 AM
  2. Pasting between sheets : Runtime 1004, Select method of Range class failed.
    By Darth269 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2012, 04:20 AM
  3. [SOLVED] Select Sheets based on variable array -ERROR 9 Subscript out of range
    By airmiles in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2012, 07:28 PM
  4. Select sheets based on what is in a range.
    By Steve in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-29-2006, 01:15 PM
  5. Trouble Using InputBox Method to Select Range from Other Sheets
    By pao_e_vinho in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-23-2006, 06:48 AM

Tags for this Thread

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