Selecting If or Else

ch46guy

New Member
Joined
Apr 29, 2013
Messages
29
I'm trying to see if a worksheet is already created. If it is Im calling UserForm2, but if its not it will create the new sheet. When I do this I get "Compile Error: Else without If". Any help would be appreciated.


Code:
For k = 1 To Worksheets.Count
        
        If Worksheets(k).Name = BorName = True Then
        UserForm2.Show
        End If
        Exit For
        
        Else
        Worksheets.Add(After:=Sheets(Sheets.Count)).Name = BorName
        Exit For
        End If
Next k
 

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,454
Try:

Code:
For k = 1 To Worksheets.Count
        
        If Worksheets(k).Name = "BorName" Then
        UserForm2.Show
        Else
        Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "BorName"
        Userfom2.Show
        End If
        
Next k
 
Last edited:

ch46guy

New Member
Joined
Apr 29, 2013
Messages
29
Thank you for your quick response. I tried that but I already have a worksheet created with the same name, which should break to Userform2, but it gives me an error that the sheet already exists.
 

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,454
Then Try,

Rich (BB code):
For k = 1 To Worksheets.Count
        
        If Worksheets(k).Name = "BorName" Then
        UserForm2.Show
        Else
        On Error Resume Next
        Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "BorName"
        Userfom2.Show
        End If
        On Error Goto 0
Next k
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,807
A loop is not needed
Code:
On Error Resume Next
If Worksheets("BorName").Name <> "BorName" Then
    Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "BorName"
End If
On Error Goto 0

UserForm2.Show
 

Forum statistics

Threads
1,082,380
Messages
5,365,124
Members
400,824
Latest member
Themilkybarkid

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