VBA add new sheet to End of Workbook

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
238
Hi all, I have a question

Here is the code to add a new sheet having the name of a textbox.value from a userform

Code:
[FONT=Arial][COLOR=#333333][FONT=Montserrat][COLOR=#000000]Sheets[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add [/COLOR][COLOR=#000000]After[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000]Sheets[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]Sheets[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Count[/COLOR][COLOR=#000000])[/COLOR][/FONT][/COLOR][/FONT]
[FONT=Arial][COLOR=#333333][FONT=Montserrat][COLOR=#000000][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=Montserrat][COLOR=#000000]Sheets.Add.Name = textbox.value[/COLOR][/FONT][/COLOR][/FONT]

I would like to know why when the code is activated, the macro creates 2 sheets, the 1st rename with the textbox value and an other one called ''sheet1"

I just would like a new sheet created with my textbox value

Any idea ?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Your code actually contains instructions to create two sheets. The first line creates a sheet, adds it to the end of the workbook, and gives it a default name. The second line creates another sheet, adds it before the active sheet since you haven't specified where to place it, and then names the sheet using the textbox value.

To create a single sheet, place it at the end of the workbook, and naming it using the textbox value, try...

Code:
Sheets.Add(After:=Sheets(Sheets.Count)).Name = TextBox1.Value

Or, alternatively...


Code:
With Sheets
    .Add(After:=.Item(.Count)).Name = TextBox1.Value
End With

Hope this helps!
 
Upvote 0
I would like to know why when the code is activated, the macro creates 2 sheets

Because you are using Sheets.Add twice
Either


Code:
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = textbox.value


or

Code:
Sub AddSht()
 Worksheets.Add(After:=Sheets(Sheets.Count)).Name = textbox.value
End Sub


Where textbox is the actual name of your textbox.


Edit: should have refreshed my screen then would have seen the previous post
:(
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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