Simple Macro to Dupe Sheet and Update Cell Value by +1 [feat. xl2bb mini-sheet]

k3yn0t3

New Member
Joined
Oct 5, 2023
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hi all. Could anyone help me with a macro that will allow me to duplicate the sheet shown below and ensure the following:
  1. Increase the Factory number in D7 by 1 each time...
  2. Rename the sheet based on the number in that cell
    1. i.e., there should be ~200 tabs named "Factory_1", "Factory_2"..."Factory_200" and so on
  3. All formulas are included when sheet is copied, and update accordingly (i.e. formulas with "Factory_1" in them update for Factory # of their new sheet)
Thank you,
Sam

Factories_v22.xlsx
CDEFGHIJKLMN
7Factory1
8
9 Q1 Q1 Q2Q2Q2Q3
10234567
11201820182018201820182018
12Transactions21,65627,81220,32719,47523,88318,354
13
14Net Sales586766544508613477
15COGS-179-232-162-152-180-139
16Gross Profit406534381357433337
17Labor-156-206-153-154-187-149
18OpEx-78-89-78-78-86-74
19Occupancy-12-16-12-12-16-12
20EBITDA159223137113144102
Factory_1
Cell Formulas
RangeFormula
I12:N12,I14:N14I12=+FILTER(INDEX('Factory Database'!$P$8:$MM$109,MATCH(Factory_1!$D$7,'Factory Database'!$A$8:$A$109,0),0),('Factory Database'!$P$6:$MM$6=Factory_1!I$11)*('Factory Database'!$P$5:$MM$5=Factory_1!I$10)*('Factory Database'!$P$7:$MM$7=Factory_1!$C12),0)
I15:N15,I17:N19I15=-+FILTER(INDEX('Factory Database'!$P$8:$MM$109,MATCH(Factory_1!$D$7,'Factory Database'!$A$8:$A$109,0),0),('Factory Database'!$P$6:$MM$6=Factory_1!I$11)*('Factory Database'!$P$5:$MM$5=Factory_1!I$10)*('Factory Database'!$P$7:$MM$7=Factory_1!$C15),0)
I16:N16I16=+I15+I14
I20:N20I20=+SUM(I16:I19)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about:
1. Put this code on D7:
=Sheet()

2. add a button and put this code on vba:

Sub DuplicatemySheet()

Dim test As Worksheet
Sheets(1).Copy After:=Sheets(Sheets.Count)
Set test = ActiveSheet
test.Name = "Factory_" & Sheets.Count

End Sub
 
Upvote 0
Maybe this way
VBA Code:
Sub MM1()
ans = InputBox("How Many Duplicate Sheets do you want?")
For r = 2 To ans
    With ActiveSheet
        .Copy After:=Sheets(Worksheets.Count)
        Range("D7") = Range("D7").Value + 1
        ActiveSheet.Name = "Factory_" & Range("D7").Value
    End With
Next r
End Sub
 
Upvote 1
Maybe this way
VBA Code:
Sub MM1()
ans = InputBox("How Many Duplicate Sheets do you want?")
For r = 2 To ans
    With ActiveSheet
        .Copy After:=Sheets(Worksheets.Count)
        Range("D7") = Range("D7").Value + 1
        ActiveSheet.Name = "Factory_" & Range("D7").Value
    End With
Next r
End Sub
Michael--thank you! Embarrassing question: how do I get this to run? I saved down as a macro-enabled workbook, went to Dev tab, clicked VBA, and added this code to my sheet and saved....

How do i start it? 🤔
 
Upvote 0
Michael--thank you! Embarrassing question: how do I get this to run? I saved down as a macro-enabled workbook, went to Dev tab, clicked VBA, and added this code to my sheet and saved....

How do i start it? 🤔
Never mind! I ran it! However, while the sheet name is updating, the hardcoded number in each new sheet is always "2" (naming is correct!), but the Factory_1 D7 cell number updates to reflect how many duplicates have been made :)
 
Upvote 0
Would it be easier to simply make a sheet with all the data and base the macro on that? This is the data to feed into that:

Factories_v23.xlsm
AB
2D7Sheet Name
31Factory_1
42Factory_2
53Factory_3
64Factory_4
75Factory_5
86Factory_6
97Factory_7
108Factory_8
119Factory_9
1210Factory_10
1311Factory_11
1412Factory_12
1513Factory_13
1614Factory_14
1715Factory_15
1816Factory_16
1917Factory_17
2018Factory_18
2119Factory_19
2220Factory_20
2321Factory_21
2422Factory_22
2523Factory_23
2624Factory_24
2725Factory_25
2826Factory_26
2927Factory_27
3028Factory_28
3129Factory_29
3230Factory_30
3331Factory_31
3432Factory_32
3533Factory_33
3634Factory_34
3735Factory_35
3836Factory_36
3937Factory_37
4038Factory_38
4139Factory_39
4240Factory_40
4341Factory_41
4442Factory_42
4543Factory_43
4644Factory_44
4745Factory_45
4846Factory_46
4947Factory_47
5048Factory_48
5149Factory_49
5250Factory_50
5351Factory_51
5452Factory_52
5553Factory_53
5654Factory_54
5755Factory_55
5856Factory_56
5957Factory_57
6058Factory_58
6159Factory_59
6260Factory_60
6361Factory_61
6462Factory_62
6563Factory_63
6664Factory_64
6765Factory_65
6866Factory_66
6967Factory_67
7068Factory_68
7169Factory_69
7270Factory_70
7371Factory_71
7472Factory_72
7573Factory_73
7674Factory_74
7775Factory_75
7876Factory_76
7977Factory_77
8078Factory_78
8179Factory_79
8280Factory_80
8381Factory_81
8482Factory_82
8583Factory_83
8684Factory_84
8785Factory_85
8886Factory_86
8987Factory_87
9088Factory_88
9189Factory_89
9290Factory_90
9391Factory_91
9492Factory_92
9593Factory_93
9694Factory_94
9795Factory_95
9896Factory_96
9997Factory_97
10098Factory_98
10199Factory_99
102100Factory_100
103101Factory_101
104102Factory_102
105103Factory_103
106104Factory_104
107105Factory_105
108106Factory_106
109107Factory_107
110108Factory_108
111109Factory_109
112110Factory_110
113111Factory_111
114112Factory_112
Sheet8
Cell Formulas
RangeFormula
B3:B114B3=+"Factory_"&A3
A4:A114A4=+A3+1
[/CODE]
 
Upvote 0
The code as provided works fine for me.
All sheets have the correct factory name and each sheet has the correct value in D7
What are you inputting at the Input box question ??
You aren't doing 1 at a time from the original sheet are you ??
 
Upvote 0
I do have the same issue; perhaps a slight modification will suffice? It seems to work for me in this form.
VBA Code:
Sub MM1()
ans = InputBox("How Many Duplicate Sheets do you want?")
For r = 2 To ans
    ActiveSheet.Copy After:=Sheets(Worksheets.Count)
    ActiveSheet.Range("D7") = r
    ActiveSheet.Name = "Factory_" & r
Next r
End Sub
 
Upvote 1
Solution
I do have the same issue; perhaps a slight modification will suffice? It seems to work for me in this form.
VBA Code:
Sub MM1()
ans = InputBox("How Many Duplicate Sheets do you want?")
For r = 2 To ans
    ActiveSheet.Copy After:=Sheets(Worksheets.Count)
    ActiveSheet.Range("D7") = r
    ActiveSheet.Name = "Factory_" & r
Next r
End Sub

This worked like a charm, Anon. Massive thanks to you & Michael.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,125
Members
453,021
Latest member
Justyna P

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