Merging vba - Sheet copy and Table of contents

exceljen86

New Member
Joined
Nov 26, 2015
Messages
15
Hi all,

I've got two sections of vba code that work well on their own, but I need to merge to make them work how I need them to:confused:.

The first section of code below looks for a Y in a particular box and copies the linked sheets into a new workbook. The second creates a table of contents.

Code:
    Sub CopySheets()    Dim rng       As Range
    Dim c         As Range
    Dim LookupLO  As ListObject
    Dim RefCol    As Object
    Dim WS        As Worksheet
    Dim wb1       As Workbook
    Dim Wb2       As Workbook
    Dim ShtStr    As String
    Dim ShtArr    As Variant
    Dim i         As Long


    Set wb1 = ThisWorkbook
    Set LookupLO = wb1.Sheets("Input").ListObjects("SheetsTbl")
    Set rng = wb1.Sheets("Input").Range("D15:D39")
    Set RefCol = CreateObject("Scripting.Dictionary")


    For Each c In rng
        If LCase(c.Value) = "y" Then RefCol(c.Address(False, False)) = vbNullString
    Next c


    For i = 1 To LookupLO.DataBodyRange.Rows.Count
        If RefCol.Exists(CStr(LookupLO.ListColumns("Cell Ref").Range(i))) Then
            On Error Resume Next
            Set WS = wb1.Sheets(LookupLO.ListColumns("Sheet Name").Range(i).Value)
            On Error GoTo 0
            If Not WS Is Nothing Then ShtStr = ShtStr & LookupLO.ListColumns("Sheet Name").Range(i) & ","
            Set WS = Nothing
        End If
    Next i


    If ShtStr <> "" Then
        ShtStr = Left(ShtStr, Len(ShtStr) - 1)
        ShtArr = Split(ShtStr, ",")
    
        Set Wb2 = Workbooks.Add
        wb1.Sheets(ShtArr).Copy Before:=Wb2.Sheets(1)
    Else
        MsgBox "No Sheets to Copy"
   
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("Sheet1").Delete
On Error GoTo 0
Application.DisplayAlerts = True


Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("Sheet2").Delete
On Error GoTo 0
Application.DisplayAlerts = True


Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("Sheet3").Delete
On Error GoTo 0
Application.DisplayAlerts = True


    End If
    Application.PrintCommunication = True


End Sub



Code:
Sub CreateTableOfContents()
    ' Determine if there is already a Table of Contents
    ' Assume it is there, and if it is not, it will raise an error
    ' if the Err system variable is > 0, you know the sheet is not there
    Dim WS As Worksheet
    On Error Resume Next
    Set WS = Worksheets("TOC")
    If Not Err = 0 Then
        ' The Table of contents doesn't exist. Add it
        Set WS = Worksheets.Add(Before:=Worksheets(1))
        WS.Name = "TOC"
    End If
    On Error GoTo 0
     
    ' Set up the table of contents page
    WS.[A2] = "Table of Contents"
    With WS.[A6]
        .CurrentRegion.Clear
        .Value = "Subject"
    End With
    WS.[B6] = "Page(s)"
    WS.Range("A1:B1").ColumnWidth = Array(36, 12)
    TOCRow = 7
    PageCount = 0
 
    ' Do a print preview on all sheets so Excel calcs page breaks
    ' The user must manually close the PrintPreview window
    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
    For Each S In Worksheets
        If S.Visible = -1 Then
            S.Select
            ThisName = ActiveSheet.Name
            HPages = ActiveSheet.HPageBreaks.Count + 1
            VPages = ActiveSheet.VPageBreaks.Count + 1
            ThisPages = HPages * VPages
 
            ' Enter info about this sheet on TOC
            Sheets("TOC").Select
            Range("A" & TOCRow).Value = ThisName
            Range("B" & TOCRow).NumberFormat = "@"
            If ThisPages = 1 Then
                Range("B" & TOCRow).Value = PageCount + 1 & " "
            Else
                Range("B" & TOCRow).Value = PageCount + 1 & " - " & PageCount + ThisPages
            End If
        PageCount = PageCount + ThisPages
        TOCRow = TOCRow + 1
        End If
    Next S
End Sub


The problem that I've got is that I need to figure out how to select the sheets, put them into the TOC and then copy them (including the TOC) to the second workbook, as the TOC code won't run on the new workbook.

Can anyone help please?

Thanks in advance
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top