VBA Create Folder on Desktop. In the below specified example we are using the Environment variable UserProfile to find the desktop location. You can change the folder path which you want to create on the desktop according to your requirement. We are checking specified folder is already exists on the desktop. If it is not available then we are creating using MkDir VBA function. In this tutorial we have explained examples and step by step instructions for your reference.
Let us see an example macro VBA Create Folder on the Desktop. In the following example we use FolderExists method. It is method of FileSystemObject(FSO) in VBA.
'VBA Create Folder on the Desktop Sub VBAF1_Create_Folder_on_Desktop() 'Variable declaration Dim sFolderName As String Dim sDesktopPath As String, sFolderPath As String 'Find Desktop path location sDesktopPath = Environ("USERPROFILE") & "\Desktop\" 'Define folder name to create on the desktop sFolderName = "Test Folder" 'Folder Path sFolderPath = sDesktopPath & sFolderName 'Create FSO Object Set oFSO = CreateObject("Scripting.FileSystemObject") 'Check Specified Folder exists or not If oFSO.FolderExists(sFolderPath) Then 'If folder is available on the desktop MsgBox "Specified folder already exists on the desktop!", vbInformation, "VBAF1" Exit Sub Else 'Create Folder MkDir sFolderPath 'Diplay messafe on the screen MsgBox "Folder has created : " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "VBAF1" End If End Sub
Output: You can find following output screenshot for your reference.
If we are not checking folder is exists or not, then it throws following error when folder is already exists. The error is File/Path access error. It is a run time error in VBA.
Note: You can find list of the environment variables in VBA by clicking on the following link.
Here are the instructions to use above macro in Visual basic editor.
- Open Visual Basic Editor(VBE) by clicking Alt +F11
- Go to code window by clicking F7
- Copy above specified macro or procedure
- Paste above copied code in code window
- Run macro by clicking F5 or Run command
- Now you can find new folder on the desktop.
- If specified folder already exists on the desktop, it displays message on the screen.
You can also learn complete details like syntax, example and etc by clicking on the following buttons.