Problems With "If Sheet Exists"

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Code:
Sub Main_Import()

Dim sht As Integer
Dim worksheetexists As String
    
'    Dim Wsht As Integer

ThisWorkbook.Unprotect

sht = Application.Sheets.Count
worksheetexists = False
    For x = 1 To sht
    If Worksheets(x).Name = "PDF2Text" Then
        worksheetsexists = True
    Exit For
    End If
    
    Next x
    If worksheetsexists = True Then
        MsgBox (" PDF2Text Tab Already Exist " _
        & vbNewLine & " Please Delete PDF2Text Tab, and Run Program Again ")
    Exit Sub
    End If
    
frm_pdfimp.Show

End Sub

Hello All,
I have the following code, and I'm having issues with defining X...among others.
I'm trying to exit the sub if the worksheet: "PDF2Text" does exist. If the sheet does not exist, then the form "frm_pdfimp" shows, and the program continues.
Is there a better way to do this?
thanks for the help
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
See line Exit Sub, added to the If statement and how to test for the sheet without looping:
Rich (BB code):
Sub Main()

    Dim x   As Long
    Dim wks As Worksheet
    Dim msg As String
    
    ThisWorkbook.Unprotect
    
    On Error Resume Next
    Set wks = Sheets("PDF2Text")
    On Error GoTo 0

    If Not wks Is Nothing Then
        msg = Replace(" PDF2Text Sheet Already Exists @NL Please Delete PDF2Text sheet and run program again ", "@NL", vbCrLf)
        MsgBox msg, vbExclamation, "PDF2Text sheet already exists"
        Set wks = Nothing
        Exit Sub
    End With

    frm_pdfimp.Show

End Sub
 
Last edited:
Upvote 0
Another option
Code:
Sub Main_Import()
   ThisWorkbook.Unprotect

    If Evaluate("isref(PDF2Text!A1)") Then
        MsgBox (" PDF2Text Tab Already Exist " _
        & vbNewLine & " Please Delete PDF2Text Tab, and Run Program Again ")
      Exit Sub
    End If
    
   frm_pdfimp.Show

End Sub
 
Upvote 0
The test in the OP is case sensitive.

Perhaps, rather than looping, you could use this to find sheetExists.

Code:
worksheetExists = False
On Error Resume Next
worksheetExists =  (LCase(Worksheets("PDF2Text").Name) = "pdf2text")
On Error Goto 0
 
Last edited:
Upvote 0
Another option
Code:
Sub Main_Import()
   ThisWorkbook.Unprotect

    If Evaluate("isref(PDF2Text!A1)") Then
        MsgBox (" PDF2Text Tab Already Exist " _
        & vbNewLine & " Please Delete PDF2Text Tab, and Run Program Again ")
      Exit Sub
    End If
    
   frm_pdfimp.Show

End Sub

This seems to be working nicely...thank you for the help
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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