VBA Creating a new sheet not working as expected

knacksc2

Board Regular
Joined
Jan 23, 2014
Messages
63
So technically the following code is working:
Code:
Private Sub CommandButton1_Click()
Create_Sheet
Import_All_Branches
Delete_Sheet
End Sub
The actual sub is this:
Code:
Sub Create_Sheet()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.Name = "Single Branch Open Order Detail"
End Sub

I have only one sheet in the workbook before i click my button, a sheet is added with the correct name but it is sheet3... If i run just the sub it creates it as sheet2.. something about using the button i assume?

here is the code for the delete sheet process:
Code:
Sub Delete_Sheet()
Application.DisplayAlerts = False
    Sheets(2).Delete
Application.DisplayAlerts = True
End Sub
Any help would be much appreciated.
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Do you have any hidden, or very hidden sheets?
 
Upvote 0
I believe no. I created this sheet today and have not hidden any sheets. not sure what very hidden means, so not sure on that one
 
Upvote 0
sometimes it works, sometimes it doesn't could it be that even after i save and close then reopen there is still memory that it already created sheet2 even though it is gone. and so just created sheet3?
 
Upvote 0
If you've closed the workbook, then the sheet numbering should revert to the next sheet number.
However if you add & delete sheets, without closing, then you'll get sheet 2, then 3, then 4 etc.
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,707
Members
452,667
Latest member
vanessavalentino83

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