VBA Code to Check if Sheet Exists and Exit Sub if Not

OilEconomist

Board Regular
Thanks in advance for any suggestions as I will give feedback accordingly.

Part of the response to my question has previously been posted, but since I've never used Functions in VBA and I am still new with VBA coding, I need some additional assistance.
https://www.mrexcel.com/forum/excel-questions/1072316-vba-check-if-sheet-exists.html

I would like to check if the following sheet in the external workbook exists, and if not, terminate the program. I only posted the pertinent code.

Code:
Sub Check_Sheet()
Dim i as Long

For i = 3 to LastRow

'The Path is "DirFolder"
'The File Name is "File_Name"

'If "Invoice Details" does not exist then (how to write this code?)

Exit Sub

EndIf

Next i

End Sub
 

Fluff

MrExcel MVP, Moderator
Something like
Code:
Public Function ShtExists(ShtName As String, Optional Wbk As Workbook) As Boolean
    If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(Wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
Sub chk()
   ShtName = "Sheet1"
   Set Wbk = Workbooks(File_Name)
   If Not ShtExists(ShtName, Wbk) Then Exit Sub
End Sub
 

OilEconomist

Board Regular
Thank you very much Fluff! Please note I have never used Functions and I am very new to writing Macros.

First question is if I make this function Private, does that basically make it to where you look at the Macros menu, it will not appear?

Second of all, since this is a closed workbook, don't I need to specify the directory?
 

Fluff

MrExcel MVP, Moderator
The function will not appear in the Macros menu, as it needs to be passed arguments.
However if you change it to Private it must be in the same module as the code that is calling it.

Also the workbook must be open.
 

OilEconomist

Board Regular
Thanks Fluff!

Understood.

How can I do it where if the workbook is closed?
 

Fluff

MrExcel MVP, Moderator
You could try this
Code:
Function ShtExistsClosed(Pth As String, Fname As String, ShtName As String)
   Dim FullPth As String
   
   FullPth = "'" & Pth & "[" & Fname & "]" & ShtName & "'!R1C1"
   ShtExistsClosed = Not IsError(Application.ExecuteExcel4Macro(FullPth))
End Function
Sub chk()
   Dim Pth As String, Fname As String, ShtName As String
   Pth = "C:\Mrexcel\"
   Fname = "+book1.xlsm"
   ShtName = "List"
   Debug.Print ShtExistsClosed(Pth, Fname, ShtName)
End Sub
But if the path or filename are wrong it will open up a dialogue box.
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top