VBA Create a worksheet from a master list and also add title (Code added)

SereneSea

New Member
Joined
Feb 2, 2022
Messages
43
Office Version
  1. 2016
Platform
  1. Windows
Hi all,
I hope you can help me::biggrin:
I am trying to create a template and use VBA code. I have two sheets, one that is a "template" and second is "master". The user will input data in the master and then use a VBA code to copy and paste the "template" as many times as needed and then name the sheet with the Title from the master sheet and use add the subtitle info in A3 going down the list in the master until a blank cell is found.

Master sheet:
Template.xlsm
ABCD
1TitleSubtitle
2Catssiamese
3DogGerman shepard
4BirdPigeon
5
6
7
8
9
10
Master

and
template:
Template.xlsm
ABCDEFGHIJ
1
2TitleInformation
3Subtitle
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Template


The code for copying and naming spreadsheet is below, but it is currently creating sheets from both A column and B Column. I only need A column to be in A2 (Title) and sheet name, and the B column info to be used in A3 (subtitle). Hope you can help me!


VBA Code:
Sub makeSheets()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Template")
Set sh2 = Sheets("Master")
    For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 2).End(xlUp))
        sh1.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value: ActiveSheet.Range("A2") = c.Value
    Next
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Change this
VBA Code:
For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 2).End(xlUp))
to
VBA Code:
For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
 
Upvote 0
That works great for pasting the title, would you be able to help me on how to get the subtitle to populate from the b column information from the master? I tried adding a similar line but I probably did it incorrectly, as it does not work .

VBA Code:
Sub makeSheets()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, d As Range
Set sh1 = Sheets("Template")
Set sh2 = Sheets("Master")
    For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
        sh1.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value: ActiveSheet.Range("A2") = c.Value
    Next
       For Each c In sh2.Range("B2", sh2.Cells(Rows.Count, 1).End(xlUp))
        sh1.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = d.Value: ActiveSheet.Range("B2") = c.Value
    Next
   
End Sub
 
Upvote 0
How about
VBA Code:
Sub makeSheets()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Template")
Set sh2 = Sheets("Master")
    For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
        sh1.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value: ActiveSheet.Range("A2").Resize(, 2).Value = c.Resize(, 2).Value
    Next
End Sub
 
Upvote 0
hmm. it still does not paste the info from column B Master to the A3 in the copied sheets.
 
Upvote 0
It should do. Does A2 on the new sheet have the sheet name in it?
 
Upvote 0
Yup that part works good, but i also need the "subtitle" to be populated with the adjacent info in B column.
 
Upvote 0
Yes the title works perfectly, but its still not filling in the Subtitle part - which for the bottom example should be Siamese as per the master sheet B column
1649778867200.png
 

Attachments

  • 1649778852898.png
    1649778852898.png
    5.4 KB · Views: 3
Upvote 0
Are you trying to put it in A3 or in B2 as per your code?
Also is anything going into B2 in the new sheet?
 
Upvote 0
I am trying to put it in A3. I removed the colors so its easier to see. nothing will be going in b3

1649779404536.png


VBA Code:
Sub makeSheets()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, d As Range
Set sh1 = Sheets("Template")
Set sh2 = Sheets("Master")
    For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
        sh1.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value: ActiveSheet.Range("A2") = c.Value
    Next
       For Each c In sh2.Range("B2", sh2.Cells(Rows.Count, 1).End(xlUp))
        sh1.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = d.Value: ActiveSheet.Range("A3") = c.Value
    Next
   
End Sub
I get a Run time error 91 - object variable with block variable not set. Me adding the second set of code does not work, as clearly I do not understand macro :{
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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