worksheets.add question

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to add a sheet but I want to keep sheet1 as an active one. I named sheet1 as "Hello". Now when I run the code below, it will do these 2 things

1) Adding a sheet before sheet1! why is that? In excel when I click + to add a sheet, it will add it after the active one not before like what vba is doing

2) the code will activate the new sheet that is created but not my first sheet which is called "Hello".

How can I fix that, I want to add a sheet but keep sheet1 activated. Thank you so much

Code:
Sub worksheet_add()
    Workbooks(1).Worksheets.Add
    Workbooks(1).Worksheets(1).Activate
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try
Code:
Sub lezawang()
   Sheets.Add , Sheets(1)
   Sheets(1).Activate
End Sub
 
Upvote 0
Here is some code I use to add sheets. You can modify as needed.

Code:
Option Explicit




Sub AddSheets()
    Dim i As Long, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Dim sname As String
    With Sheets("Sheet1")
        For i = 1 To lr
            sname = .Range("A" & i)
            Sheets.Add After:=Sheets(Sheets.Count)
            Sheets(Sheets.Count).Name = sname
        Next i
    End With
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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