VBA Help - Multiple If/ElseIf Statements

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
578
Office Version
2016
Platform
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?
 

Nine Zero

Well-known Member
Joined
Mar 10, 2016
Messages
622
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,323
Office Version
365
Platform
Windows
Wait one
 
Last edited:

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
578
Office Version
2016
Platform
MacOS
@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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,323
Office Version
365
Platform
Windows
I realised that what I had posted was wrong & then before I could post again, got caught up in something else.
 

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
578
Office Version
2016
Platform
MacOS
Oh ok, no problem. Always appreciate your help on these projects.

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

Forum statistics

Threads
1,082,334
Messages
5,364,677
Members
400,810
Latest member
elbashka

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top