VBA add new sheet to End of Workbook

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
235
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 ?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,004
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!
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
11,481
Office Version
365, 2010
Platform
Windows, Mobile
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:

Forum statistics

Threads
1,081,748
Messages
5,361,041
Members
400,610
Latest member
ebey

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top