creating a new sheet in different workbook

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,565
Hi
The code below is working. I have 2 workbooks. Workbooks(1) has the code. When the user enter x = 2
the code below will create a worksheet in Workbooks(2) after worksheets(1). It is working but I wonder, in line 4, why I did not have to do this

Rich (BB code):
Workbooks(x).Worksheets.Add after:=Workbooks(x).Worksheets(1)
It will create the sheet in workbooks(2) anyway. Is that because of Workbooks(x=2).worksheets.Add? Thank you very much.


Rich (BB code):
Sub ws_add_using_collection()
    Dim x As Integer
    x = InputBox("which book")
    Workbooks(x).Worksheets.Add after:=Worksheets(1)
End Sub
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,668
Office Version
365
Platform
Windows
Code:
after:=Worksheets(1)
Just tells it where to add the new sheet & as there is always at least 1 sheet, it will work.
If you tried to add a sheet as the last sheet (ie sheets.count) you might get an error, or the new sheet might be in the wrong place.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,345
Messages
5,486,317
Members
407,539
Latest member
ltwkuav

This Week's Hot Topics

Top