Using VBA to create a new workbook from a work template

SidWallace

New Member
Joined
Sep 15, 2014
Messages
2
Hi

I am not too good with VBA. I have a template that has a summary and a number of tabs that are required to be completed based on what is marked in the summary. From the summary i would like to create a new workbook that only has the relevant sheets.

i.e. My Summary would have in column A the name of the sheets and B if they are relevant for this bit of work this is based off a data file that is saved in the workbook. All sheets would be set up in the workbook. In the new workbook all that is required to be created is the Summary, the data file and the relevant sheets that have a Y in column B. In the example below I would only require sheets for the Summary, the data file and SheetA, SheetC and SheetE.

Is this possible?
AY
BN
CY
DN
EY

<COLGROUP><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2624" width=82><COL style="WIDTH: 54pt" width=72><TBODY>
</TBODY>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
If you include the Summary Sheet and Data sheet names in your table:

Excel 2010
AB
1SheetRequired
2Summaryy
3Datay
4Sheet3n
5Sheet4y
6Sheet5n
7Sheet6y

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Summary



Could can do something like the following:

Code:
Sub CreateWorkbook()


Dim rng As Range
Dim i As Long
Dim SheetsArr() As String
Dim SheetCount As Long
Dim wb As Workbook


Set rng = ActiveWorkbook.Sheets("Summary").UsedRange
SheetCount = 1


For i = 2 To rng.Rows.Count
    If Contains(Sheets, rng.Cells(i, 1)) And LCase(rng.Cells(i, 2).Value) = "y" Then
        ReDim Preserve SheetsArr(1 To SheetCount)
        SheetsArr(SheetCount) = rng.Cells(i, 1)
        SheetCount = SheetCount + 1
    Else
    'What would you like to do if a sheet marked as y doesnt exist?
    End If
Next i


ActiveWorkbook.Sheets(SheetsArr).Copy
Set wb = ActiveWorkbook


'The rest of your code to manipulate the new workbook goes here


End Sub
Function Contains(objCollection As Object, strName As String) As Boolean
    Dim o As Object
    On Error Resume Next
    Set o = objCollection(strName)
    Contains = (Err.Number = 0)
 End Function
 
Upvote 0

Forum statistics

Threads
1,222,032
Messages
6,163,511
Members
451,839
Latest member
HonestZed

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