Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Checking if Folder Exists with VBA

April 27, 2020 Marcus Small
Photo by Laika Notebooks on Unsplash

Photo by Laika Notebooks on Unsplash

I was updating my popular blog post Create a Folder with VBA and I realised while I was making the video I had left out a crucial element - what if the folder already exists. This will cause the code to bug out and this is not desirable. So in order to address that we can trap the existence of a folder.

The following short video shows you how to create a folder with VBA and how to avoid an error if the folder already exists. The following Excel file is the output from the video.

Check Folder Exists.xlsm

Avoiding errors in code is highly desirable. and the code to do that when making a folder is about using the combination of the Dir command and the MkDir command.



The Dir will either produce a ““ null result if the folder exists.



Or it will produce a period “.” because of the Boolean nature of this result we can trap the null result and create a new folder and ignore the period result meaning nothing happens.

Sub FolderVBA2()
'Purpose: Check if folder exists - Source TheSmallman.com
Dim i As Integer

  For i = 1 to 5
     str = "C:\MyFiles\" & Range("A" & i) & "\"
     fol = Dir(str, vbDirectory)
    IF fol = "" Then MkDir "C:\MyFiles\" & Range("A" & i)
  Next i

End Sub

The code to check if a folder exists is as follows:

In the above example there are 5 folders to check. If any of the 5 folders exists then the code will do nothing. If on the other hand the folder does not exist then a new folder is created.

The string either produces “ “ or “.” as there are only two possibilities the IF statement can test for “ “ (blank) and the new folder is make with the MkDir statement.



Rookie error

"\"



Remember to include the back slash in the code for the Str (string) if you leave it out all sorts of problems occur. We could test for its existence but just do the right thing : )

The following Excel file is from the video and has all the examples inside of it.


Check Folder Exists.xlsm

← Transfer Data Between Arrays with VBAScripting Dictionary with Ranges →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.