VBA Help - Multiple If/ElseIf Statements

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hi guys,

I am working on a project and found myself spinning in circles with this code. Hope someone can steer me in the right direction.

Background:I have a menu grid with the name of 4 sheets that a user can have added to a workbook based on "True" or "False" from a drop down next to the sheet name. I need to build a If statement that can dynamically add or not add the sheet based on the option the user selects.

Example:
Sheet1 = False
Sheet2 = True
Sheet3 = False
Sheet4 = False

Desired Output:Code would loop thru the if statements and see that Sheet2 is the only Sheet that needs to be added.

Format of what I need:

Code:
Sub 4Statements ()

'Sheet1-sheet4 are named ranges I created to look for the true or false value next to the sheet name

If Range("sheet1").value = True then                                       'Do what you need to do to create the sheet
elseif

'Skip to the next sheet option 
If Range("sheet2").value = True then                                       'Do what you need to do to create the sheet
elseif

If Range("sheet3").value = True then                                       'Do what you need to do to create the sheet
elseif

If Range("sheet4").value = True then                                       'Do what you need to do to create the sheet
elseif

'End if  '(x4 I would assume?)

End Sub

Sound about right?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hey,

See if you can do something with this.... my range for the true and false values is A1 thru A4 and my dropdowns for the sheet names are in B1 thru B4

Tested and works

Code:
Sub CreateSheets()
Dim yo As Range
Dim nameofsheet As String
For Each yo In Range("A1:A4")
    If yo.Value = True Then
        nameofsheet = yo.Offset(, 1).Value
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = nameofsheet
    End If
Next

End Sub
 
Upvote 0
Wait one
 
Last edited:
Upvote 0
@Nine Zero I will give this a try today. Thanks for the suggestion!

Hey,

See if you can do something with this.... my range for the true and false values is A1 thru A4 and my dropdowns for the sheet names are in B1 thru B4

Tested and works

Code:
Sub CreateSheets()
Dim yo As Range
Dim nameofsheet As String
For Each yo In Range("A1:A4")
    If yo.Value = True Then
        nameofsheet = yo.Offset(, 1).Value
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = nameofsheet
    End If
Next

End Sub
 
Upvote 0
I realised that what I had posted was wrong & then before I could post again, got caught up in something else.
 
Upvote 0

Forum statistics

Threads
1,224,271
Messages
6,177,619
Members
452,786
Latest member
k3calloway

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