How to create folder in desktop with vba whenever the excel book is opened, if exist, ignore?

viriato74

New Member
Joined
Aug 20, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
How to create folder in desktop with vba whenever the excel book is opened, if exist, ignore.
Message if create new folder, silent if the folder exist.

I have the code to create folder on desktop
Sub CriarPastaFaturaDesktop()
'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 = "FATURAS"

'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 "Uma pasta com o mesmo nome já existe no Desktop!", vbInformation, "INFO"
Exit Sub
Else
'Create Folder
MkDir sFolderPath

'Diplay messafe on the screen
MsgBox "Pasta criada com sucesso em: " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "INFO"
End If

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Gokhan Aycan

Active Member
Joined
Aug 8, 2021
Messages
399
Office Version
  1. 365
Platform
  1. Windows
Insert a module, paste the code to it and call this sub from Workbook_Open event.

Double click on "ThisWorkbook"

1629514356451.png


I don't have a Module1 but say it is there.

1629514754316.png
 

viriato74

New Member
Joined
Aug 20, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Thanks for your help.
I found what I was looking for.

VBA Code:
Private Sub Workbook_Open()

        Dim cOb As Variant
        Dim FolderName As String, FolderExists As String
        FolderName = "C:\Users\" & Environ("username") & "\Desktop\[B]MyFolder[/B]\" '---->Change folder name to suit. Change the AAAAA to your requirement.
        FolderExists = Dir(FolderName, vbDirectory)
    
Application.ScreenUpdating = False
       
       If FolderExists = vbNullString Then
            MsgBox "The desktop folder doesn't exist. Creating a new folder now.", vbExclamation, "INFORMATION"
            cOb = CreateObject("wscript.shell").specialfolders("Desktop") & "\" & "[B]MyFolder[/B]" '--->Change folder name to suit.
            MkDir cOb
            Else: Exit Sub
       End If
    
Application.ScreenUpdating = True

End Sub
 

renrut_5

New Member
Joined
Sep 28, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am having similar issues and was wondering if any further help could be warranted. I have attached my in house file in hopes that someone can assist. I need to be able to use this spreadsheet twice for each "RMA". One for inbound pictures and one for outbound pictures. These two access times may be as much as thee to six weeks apart depending on customer payment, repair time, etc. Right now if I keep the spreadsheet open the two will open with no issues. However, if I close the spreadsheet and then try to go back to an already posted RMA I get a "path not found" error. Please assist.
 

Attachments

  • exceltext.JPG
    exceltext.JPG
    17.5 KB · Views: 3
  • Userform1.JPG
    Userform1.JPG
    43 KB · Views: 3
  • Userform1text.JPG
    Userform1text.JPG
    103.6 KB · Views: 3
  • Userform2.JPG
    Userform2.JPG
    28 KB · Views: 4
  • Userform2text.JPG
    Userform2text.JPG
    37.9 KB · Views: 3

Forum statistics

Threads
1,147,564
Messages
5,741,856
Members
423,691
Latest member
Fahad987

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
Top