VBA to input new sheets to workbook

zakynthos

Board Regular
Joined
Mar 28, 2011
Messages
169
How would I modify this code to populate the workbook I'm using with variable no of new worksheets (not into a new workbook as below?

I would like to add 100 tabs with the generic name Recipient' after the last existing tab in the workbook

Many thanks!

Code:
'DECLARE VARIABLES
'Stores the users preferred no. of sheets and names workbook so we can refer back to itAllows us to loop through the new sheets and rename them
Dim lngPref  As Long, wkbNew As Workbook
Dim strPrefix As String, intSheets As Integer, i As Integer

'CAPTURE USER PREFERENCES
'Populates lngPref with application sheets in new workbook
'Ensures the 4 sheets set application to 4
'We create a new wkb workbook inside our new  wkb variable

lngPref = Application.SheetsInNewWorkbook
strPrefix = InputBox("Please enter your prefix", "New Workbook")
intSheets = InputBox("Number of sheets required", "New Workbook")

'CREATE WORKBOOK CONTAINING 4 WORKSHEETS
Application.SheetsInNewWorkbook = intSheets
Set wkbNew = Application.Workbooks.Add

'NAME NEW SHEETS IN WORKBOOK
'We use the For ..Next loop to rename the sheets in our new workbook
'To create the name we concatenate the current value with our integer variable
'The integer variable is used both to target the worksheet and compose the name

For i = 1 To intSheets
wkbNew.Worksheets(i).Name = strPrefix & "month1" & i
Next

'RESTORE USER PREFERENCES
'We simply reverse the line of code we used to capture user's preferences in the first place:

Application.SheetsInNewWorkbook = lngPref

'Finally , we 'll allow the user to choose the prefix for the new worksheets as well as the number of sheets.  We'll need to declare 2 more variables to do this:
'Dim strPrefix As String, intSheets As Integer

'We populate both these variables using the inputbox statement:
'strPrefix = InputBox("Please enter your prefix", "New Workbook")
'intSheets = InputBox("Number of sheets required", "New Workbook")
'we also need to change the no,. of sheets in the new workbook from 4 to 'intsheets'
'Finally, we need to replace the 2 fixed values in our For loop:
'The 4 is replaced with intSheets and Quarter is replaced with strPrefix

'To include a space between week and integer we would simply add an &  "" after strPrefix
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
may be this code will help
Code:
Sub Test()
Dim ShNeed As Integer, i As Integer
ShNeed = InputBox("Insert number of sheets you want ")
For i = 1 To ShNeed
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Recipient " & Sheets.Count + 1
Next i
End Sub
 
Upvote 0
The short answer is: I wodn't modify that code at all - I'd write some from scratch. You just need something like this:-
Code:
[FONT=Fixedsys]Dim x[/FONT]
 
[FONT=Fixedsys][FONT=Fixedsys]With ThisWorkbook[/FONT]
  For x = 1 To 100[/FONT]
[FONT=Fixedsys]   .Worksheets.Add After:=.Worksheets(.Worksheets.Count)[/FONT]
[FONT=Fixedsys]   .Worksheets(.Worksheets.Count).Name = "Recipient " & x[/FONT]
[FONT=Fixedsys] Next x[/FONT]
[FONT=Fixedsys] .Worksheets(1).Activate[/FONT]
[FONT=Fixedsys]End With[/FONT]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,541
Messages
6,179,418
Members
452,912
Latest member
alicemil

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