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 Excel 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.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.