Sub File_Exist_With_Dir()
Application.ScreenUpdating = False
Dim FilePath As String
FilePath = ""
On Error Resume Next
FilePath = Dir("C:\MyFolder\MyDocuments\Isele\ProductList.xlsx")
On Error GoTo 0
If FilePath = "" Then
MsgBox "File doesn't exist", vbInformation, "File not found"
Exit Sub
Else
MsgBox "File exist", vbInformation, "File exists"
Call MyNewFile
End If
Application.ScreenUpdating = False
End Sub
Sub MyNewFile()
MsgBox ("hello")
End Sub
Option Explicit
Sub File_Exist_With_Dir()
Dim oWb As Workbook
Dim FilePath As String
Const ShtNm As String = "Overall_List"
FilePath = ""
On Error Resume Next
FilePath = Dir("C:\MyFolder\MyDocuments\Isele\ProductList.xlsx")
On Error GoTo 0
If FilePath = "" Then
MsgBox "File doesn't exist", vbInformation, "File not found"
Exit Sub
Else
On Error GoTo the_end
Application.ScreenUpdating = False
Set oWb = Workbooks.Open(FilePath)
If WksExists(ShtNm) Then MyNewFile
the_end:
Application.ScreenUpdating = False
End Sub
Function WksExists(wksName As String) As Boolean
On Error Resume Next
WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
SheetFound = Evaluate("=ISREF('" & "Overall_List" & "'!A1)")
Regards
ASKER
ASKER
Dim app As Excel.Application
Set app createObject("Excel.Application")
'// open your workbook
Dim wb As Excel.Workbook
Set wb = app.Workbooks.open "c:\ .........xlsx"
If(existInCollection("Overall_List", wb) Then
'// worksheet exist, do Something here
Else
'// worksheet does not exist, do Something else here.
End If
Public function existInCollection(byval key As String, byRef col As Collection) As Boolean
existInCollection = existInCollectionByVal(key, col) or existInCollectionByRef(key, col)
End Function
'// check if the item exist as a value
Private function existInCollectionByVal(byval key As String, byRef col As Collection) As Boolean
On Error goto Error
Dim item As Variant
item = col(key)
existInCollectionByVal = True
Exit Function
Error:
existInCollectionByVal = False
End Function
'// Check if the item exist as a reference
Private function existInCollectionByRef(byval key As String, byRef col As Collection) As Boolean
On Error goto Error
Dim item As Variant
Set item = col(key)
existInCollectionByVal = True
Exit Function
Error:
existInCollectionByVal = False
End Function
Sub FileAnd SheetExists()
Dim oWb As Workbook
Dim FilePath As String
Const ShtNm As String = "Overall_List"
FilePath = ""
On Error Resume Next
FilePath = Dir("C:\MyFolder\MyDocuments\Isele\ProductList.xlsx")
On Error GoTo 0
If FilePath = "" Then
MsgBox "File doesn't exist", vbInformation, "File not found"
Exit Sub
Else
Set oWb = Workbooks.Open(FilePath)
If Evaluate("=ISREF('" & ShtNm & "'!A1)") Then
MyNewFile
Else
oWb .Close False
Endif If
End Sub
ASKER
ASKER
ASKER
Option Explicit
Sub File_Exist_With_Dir()
Dim oWb As Workbook
Dim FilePath As String
Const ShtNm As String = "Overall_List"
FilePath = ""
On Error Resume Next
FilePath = Dir("C:\MyFolder\MyDocuments\Isele\ProductList.xlsx")
On Error GoTo 0
If FilePath = "" Then
MsgBox "File doesn't exist", vbInformation, "File not found"
Exit Sub
Else
On Error GoTo the_end
Application.ScreenUpdating = False
Set oWb = Workbooks.Open(FilePath)
If WksExists(ShtNm) Then
MyNewFile '' 'calls your Procedure
Else MsgBox ShtNm & " does not exist in this workbook"
End If
the_end:
Application.ScreenUpdating = False
End Sub
Function WksExists(wksName As String) As Boolean
On Error Resume Next
WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
ASKER
ASKER
Option Explicit
Sub File_Exist_With_Dir()
Const ShtNm As String = "Overall_List"
If WksExists(ShtNm) Then
'MyNewFile '' 'calls your Procedure
MsgBox "Sheet exists"
Else: MsgBox ShtNm & " does not exist in this workbook"
End If
the_end:
Application.ScreenUpdating = False
End Sub
Function WksExists(wksName As String) As Boolean
On Error Resume Next
WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
Open in new window