VBA Code to copy selected tabs to new separate new workbooks, Hardcode, then Save As

CDave14

New Member
Joined
Jun 19, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hey team,
I have a master/template sheet that has multiple tabs some of which only apply in certain circumstances.
Could I please get some help in writing a VBA which moves Sheets [lets name them (S1, S2, S3...)] to a new book, hardcodes all the values then saves with a file name based on a cell values (A1 & B2).
E.g. I'd like to create a new book with S1 & S3 then hardcode and save and create another new book with S2 & S3 then hardcode and save, and so on.
Fairly new to VBA so let me know if you need more info

Thanks in advance.
 

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.
How do you intend to 'select' which tabs you want to copy to a new workbook? Will they always be the same combination?
How will you 'build' the new file name from the values in cells A1 & B2, and are they on the 'master/template' sheet?
Do you want to save the new workbook to the same folder that the master workbook is in?
 
Upvote 0
They will be the same combination of sheets to copy to new book and the sheet names in the master will be static , so was planning to select/identify by the sheet name e.g. Copy S1 & S3 to a new book to create the new book

File name will be the same as the master with values in A1 (date) & B2 (identifier) added to the end on a menu sheet where the macro will be run from.

Cheers Kevin
 
Upvote 0
File name will be the same as the master with values in A1 (date) & B2 (identifier) added to the end on a menu sheet where the macro will be run from.
That bit confuses me ;)
What exactly is the "menu sheet"? Is this at the end of the master workbook, and do you intend to keep a running list of copies made?
Please answer these questions, but in the meantime I'll start putting together some code that I would use if I had a similar project.
 
Upvote 0
The following is provided purely for demonstration purposes, showing the various steps I would take to do this kind of project. I get the impression that there's not dozens of possible combinations of sheets, therefore you could hardcode the actual sheet names for each macro & assign to different buttons perhaps? Otherwise, there are more complicated ways of selecting sheet combinations. I've kept the code deliberately simple to be easy to adjust to your actual requirements. Please try it on a copy of your workbook. This one looks to add sheets S1 and S3 to a new workbook.

VBA Code:
Option Explicit
Sub Test_S1_and_S3()
    
    'Declare variables
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet, wb As Workbook
    Dim s As String, i As Long
    Set ws1 = Worksheets("master")
    Set ws2 = Worksheets("menu")
    
    'Build the new workbook name
    s = ActiveWorkbook.Name
    If InStr(s, ".") > 0 Then
       s = Left(s, InStr(s, ".") - 1)
    End If
    s = s & " " & Format(ws1.Range("A1"), "dd-mm-yyyy") & " " & ws1.Range("B2").Value
    
    'Select the sheets and copy to a new workbook
    With ThisWorkbook.Sheets(Array("S1", "S3"))     '<~~ *** change sheet names for other macros ***
        .Copy
    End With
    Set wb = ActiveWorkbook
    
    'Convert formulas to constants
    With wb.Sheets(1).UsedRange
        .Value = .Value
    End With
    With wb.Sheets(2).UsedRange
        .Value = .Value
    End With
    
    'Save the new workbook with a new name
    wb.SaveAs ThisWorkbook.Path & "\" & s
    
    'Add the newly created file name to the next available cell in column A of the menu sheet
    ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1).Value = s

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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