add new sheets & incerement numbers using inputbox

Ali M

Active Member
Joined
Oct 10, 2021
Messages
287
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
hello

I have first sheet name's MAIN . what I want using input box if i write 1 , then should add new sheet name's PURCHASE1 after the first sheet and if I write 2 in inputbox then should add new sheets names are PURCHASE2,PURCHASE3 and so on . to become the final result after sheet MAIN (PURCHASE1,PURCHASE2,PURCHASE3)

so every time write value into inputbox should add and inceremnts numbers based on previous sheet name.
thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
This macro assumes that your workbook starts out with only one sheet.
VBA Code:
Sub AddSheets()
    Application.ScreenUpdating = False
    Dim wsNum As String, x As Long, y As Long
    wsNum = InputBox("Please enter the number of sheet to create.")
    If Sheets.Count = 1 And wsNum = 1 Then
        Sheets.Add(after:=Sheets(Sheets.Count)).Name = "PURCHASE1"
        Exit Sub
    Else
        If Sheets.Count = 1 Then
            Sheets.Add(after:=Sheets(Sheets.Count)).Name = "PURCHASE1"
            For y = 2 To wsNum
                x = Mid(Sheets(Sheets.Count).Name, 9, 9999)
                Sheets.Add(after:=Sheets(Sheets.Count)).Name = "PURCHASE" & x + 1
            Next y
        Else
           For y = 1 To wsNum
                x = Mid(Sheets(Sheets.Count).Name, 9, 9999)
                Sheets.Add(after:=Sheets(Sheets.Count)).Name = "PURCHASE" & x + 1
            Next y
        End If
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi,
Couple of ideas

- if you are just adding Purchase sheets in sequence after the "Main" sheet then maybe no need for InputBox & something like this will do what you want

VBA Code:
Sub AddPurchaseSheet()
    Dim NextNo As Long
                              
    NextNo = Val(Mid(Worksheets(Sheets.Count).Name, 9)) + 1
    Worksheets.Add(after:=Worksheets(Sheets.Count)).Name = "PURCHASE" & NextNo
End Sub

If though want an Input option & again, assuming Purchase sheets remain in sequence, then may this

VBA Code:
Sub AddPurchaseSheet()
    Dim IndexNo     As Variant
    Dim SheetExists As Boolean
    Dim Default     As Long
    
    
    Do
        If SheetExists Then MsgBox "PURCHASE" & IndexNo & Chr(10) & _
                                    "Sheet Name Exists", 48, "Sheet Exists"
                                    
        Default = Val(Mid(Worksheets(Sheets.Count).Name, 9)) + 1
                                    
        IndexNo = InputBox("Enter Next Sheet Number", "Add New Sheet", Default)
        'cancel pressed
        If StrPtr(IndexNo) = 0 Then Exit Sub
        'check if sheet exists
        SheetExists = Evaluate("ISREF('PURCHASE" & Val(IndexNo) & "'!A1)")
    Loop Until Val(IndexNo) > 0 And Not SheetExists
    Worksheets.Add(after:=Worksheets(Sheets.Count)).Name = "PURCHASE" & IndexNo
End Sub

Dave
 
Upvote 0
You are very welcome. :)
 
Upvote 0
@dmt32 great work !
just the second macro you seem misunderstood when eneter value like 3 . it just add purchase3 , but what I want should add three sheets at once
should be purchase1 purchase2,purchase3 . and if enter value 1 then should add purchase4 , then the final result could be purchase1 purchase2,purchase3,purchase4
anyway thanks for theses choices .;)
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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