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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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:
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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