Create new sheets and name based off cell value

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
723
Office Version
  1. 2016
Platform
  1. Windows
I have a bit of code -
VBA Code:
strArea = InputBox("Area? (80,82,83,84,87)")
strQtr = InputBox("Quarter? (1,2,3,4)")
    If strArea = "80" Then
        Area80 strQtr
    ElseIf strArea = "82" Then
        Area82 strQtr
    ElseIf strArea = "83" Then
        Area83 strQtr
    ElseIf strArea = "84" Then
        Area84 strQtr
    ElseIf strArea = "87" Then
        Area87 strQtr
    End If
I need some help adding in something like the following as an example. I can't figure out how to get the formula to work with Sheets.Add.Name.
VBA Code:
    If strArea = "80" Then
        Area80 strQtr
Create four new worksheets
Sheets.Add.Name = "first "& =Left('Dealer Status'!B4,2)
Sheets.Add.Name = "second" & =Left('Dealer Status'!B4,2)
Sheets.Add.Name = "third" & =Left('Dealer Status'!B4,2)
Sheets.Add.Name = "fourth" & =Left('Dealer Status'!B4,2)
    ElseIf strArea = "82" Then
        Area82 strQtr
Create eight new worksheets
Sheets.Add.Name = "first "& =Left('Dealer Status'!C5,3)
Sheets.Add.Name = "second" & =Left('Dealer Status'!C5,3)
Sheets.Add.Name = "third" & =Left('Dealer Status'!C5,3)
Sheets.Add.Name = "fourth" & =Left('Dealer Status'!C5,3)
Sheets.Add.Name = "first "& =Left('Dealer Status'!C5,14)
Sheets.Add.Name = "second" & =Left('Dealer Status'!C5,14)
Sheets.Add.Name = "third" & =Left('Dealer Status'!C5,14)
Sheets.Add.Name = "fourth" & =Left('Dealer Status'!C5,14)

Thank you as always for everyone's generosity.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This:
VBA Code:
Sheets.Add.Name = "first "& =Left('Dealer Status'!B4,2)
should look like this:
VBA Code:
Sheets.Add.Name = "first " & Left(Sheets("Dealer Status").Range("B4"), 2)

Also, instead of using all those "ElseIf" statements, just use a Case statement, i.e. this structure:
VBA Code:
    If strArea = "80" Then
        Area80 strQtr
    ElseIf strArea = "82" Then
        Area82 strQtr
    ElseIf strArea = "83" Then
        Area83 strQtr
    ElseIf strArea = "84" Then
        Area84 strQtr
    ElseIf strArea = "87" Then
        Area87 strQtr
    End If
can be simplified to this:
VBA Code:
    Select Case strArea
        Case "80"
            Area80 strQtr
        Case "82"
            Area82 strQtr
        Case "83"
            Area83 strQtr
        Case "84"
            Area84 strQtr
        Case "87"
            Area87 strQtr
    End Select
See: MS Excel: How to use the CASE Statement (VBA)
 
Upvote 0
Solution
I found the following, but get an error Variable not defined.
VBA Code:
'VBA Add New Worksheet and Specify Name from Cell
Sub VBAF1_Add_Sheet_Specify_Name_From_Cell()
     
    'Specify Name to Sheet from cell
    Sheets.Add.Name = Sheet1.Range("B1")

End Sub
Source = Create New Worksheet in a Workbook VBA Create Worksheet in Excel
This:
VBA Code:
Sheets.Add.Name = "first "& =Left('Dealer Status'!B4,2)
should look like this:
VBA Code:
Sheets.Add.Name = "first " & Left(Sheets("Dealer Status").Range("B4"), 2)

Also, instead of using all those "ElseIf" statements, just use a Case statement, i.e. this structure:
VBA Code:
    If strArea = "80" Then
        Area80 strQtr
    ElseIf strArea = "82" Then
        Area82 strQtr
    ElseIf strArea = "83" Then
        Area83 strQtr
    ElseIf strArea = "84" Then
        Area84 strQtr
    ElseIf strArea = "87" Then
        Area87 strQtr
    End If
can be simplified to this:
VBA Code:
    Select Case strArea
        Case "80"
            Area80 strQtr
        Case "82"
            Area82 strQtr
        Case "83"
            Area83 strQtr
        Case "84"
            Area84 strQtr
        Case "87"
            Area87 strQtr
    End Select
See: MS Excel: How to use the CASE Statement (VBA)
Thank you so very much. I knew I was close :)
Also, thank you for the tip with using Case instead of If.
 
Upvote 0
You are welcome.

I found the following, but get an error Variable not defined.
If you had deleted the first sheet in the workbook at some point, you would no longer have "Sheet1", and would get that error message,
 
Upvote 0

Forum statistics

Threads
1,216,111
Messages
6,128,899
Members
449,477
Latest member
panjongshing

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