If statement to check skip to "else" if path does not exist (open)

sdoppke

Well-known Member
Joined
Jun 10, 2010
Messages
647
Hi everyone I have the below macro that needs help checking if the file to open is opens or not, if it does not then needs to skip to the else.

Thanks anyone for any help. :)

Code:
Sub Place_Path()
Dim wb As Workbook
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Schedule Dashboard").Range("F3")
fPath = ("[URL]http://abcdefg/[/URL]" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
sstring = Replace(fPath, " ", "%20")
Sheets("MyStoreInfo").Range("G2") = sstring
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Workbooks.Open(Filename:=sstring)
'Here i need to create something here that tries to open the above "sstring", If it does not exist then the "Else" statement below
For Each wb In Application.Workbooks            ' Loop through each workbook
        If wb.Name <> ThisWorkbook.Name Then        ' Exclude this workbook
            For Each ws In wb.Sheets                ' Loop through each worksheet of each workbook
                If ws.Name = "Week 1" Then
                Sheets("Week 1").Select
                Sheets("Week 1").Range("AT1").Copy
                ThisWorkbook.Activate
                Sheets("Schedule Dashboard").Select
                Range("W11").Select
                ActiveSheet.Paste
            End If
        Next ws
    End If
Next wb
'Else Sheets("Schedule Dashboard").Range("W11") = "have not"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Something like this:
Code:
Option Explicit
 
Sub Place_Path()
Dim wb As Workbook
Dim ThisFile As String, Area As String, Region As String
Dim District As String, M0nth As String, fPath As String, sString As String
 
With Sheets("MyStoreInfo")
    ThisFile = .Range("C2")
    Area = .Range("E8")
    Region = .Range("F8")
    District = .Range("C8")
    M0nth = Worksheets("Schedule Dashboard").Range("F3")
    fPath = ("http://abcdefg/" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
    sString = Replace(fPath, " ", "%20")
    .Range("G2") = sString
End With
 
Application.ScreenUpdating = False
Application.DisplayAlerts = False
 
If Len(Dir(sString)) > 0 Then    'this verifies the file exists
    Set wb = Workbooks.Open(Filename:=sString)
    wb.Sheets("Week 1").Range("AT1").Copy _
        ThisWorkbook.Sheets("Schedule Dashboard").Range("W11")
    wb.Close False
Else
    Sheets("Schedule Dashboard").Range("W11") = "have not"
End If
 
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
Upvote 0
Something like this:
Code:
Option Explicit
 
Sub Place_Path()
Dim wb As Workbook
Dim ThisFile As String, Area As String, Region As String
Dim District As String, M0nth As String, fPath As String, sString As String
 
With Sheets("MyStoreInfo")
    ThisFile = .Range("C2")
    Area = .Range("E8")
    Region = .Range("F8")
    District = .Range("C8")
    M0nth = Worksheets("Schedule Dashboard").Range("F3")
    fPath = ("http://abcdefg/" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
    sString = Replace(fPath, " ", "%20")
    .Range("G2") = sString
End With
 
Application.ScreenUpdating = False
Application.DisplayAlerts = False
 
If Len(Dir(sString)) > 0 Then    'this verifies the file exists
    Set wb = Workbooks.Open(Filename:=sString)
    wb.Sheets("Week 1").Range("AT1").Copy _
        ThisWorkbook.Sheets("Schedule Dashboard").Range("W11")
    wb.Close False
Else
    Sheets("Schedule Dashboard").Range("W11") = "have not"
End If
 
Application.ScreenUpdating = True
End Sub
you rock thanks!
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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