Checks if folder exists at the given address [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi team,

I have been using this code (see below) to check if Excel.xlsm exists at given address. This code works perfectly, without any problem.

VBA Code:
Public Sub Open_Excel(ByRef control As Office.IRibbonControl)

'Checks if Excel.xlsm exists at the given address
Dim FilePath As String
Dim TestStr As String
FilePath = "P:\ADDRESS\ADDRESS\Excel.xlsm"
TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
MsgBox "Does not exist.", vbExclamation
Exit Sub
Else
End If

'Open Excel.xlsm
With Application
.ScreenUpdating = False
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks("Excel.xlsm")
On Error GoTo 0
If wb Is Nothing Then
Set wb = Workbooks.Open("P:\ADDRESS\ADDRESS\Excel.xlsm")
End If
.ScreenUpdating = True
End With

End Sub

Now I want to add something similar to the code below.

As you can see in the code above, under the line 'Checks if Excel.xlsm exists at the given address, but in this case for a folder.

Basically I want to check if specific folder named Folder exists at the given address before actually opening Folder.

VBA Code:
Public Sub Open_Folder(ByRef control As Office.IRibbonControl)

'Open Folder
With Application
.ScreenUpdating = False
Dim strFolder As String
strFolder = "P:\ADDRESS\ADDRESS\Folder"
ActiveWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True
.ScreenUpdating = True
End With

End Sub

I hope it is clear.

Thank you.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Solved.

VBA Code:
Public Sub Open_Folder(ByRef control As Office.IRibbonControl)

'Checks if folder exists at the given address
Dim sFolderPath As String
sFolderPath = "P:\ADDRESS\ADDRESS\Folder"
If Right(sFolderPath, 1) <> "\" Then
sFolderPath = sFolderPath & "\"
End If
If Dir(sFolderPath, vbDirectory) <> vbNullString Then

'Open Folder
With Application
.ScreenUpdating = False
Dim strFolder As String
strFolder = "P:\ADDRESS\ADDRESS\Folder"
ActiveWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True
.ScreenUpdating = True
End With

Else

MsgBox "Does not exist.", vbExclamation
End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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