VBA create folder help

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to create a folder from an excel in using the following code:

VBA Code:
Sub Creaolders()

  Dim sPath As String, sMain As String, sFolder As String
  Dim i As Long
 
  sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
  sMain = sPath & "\" & Sheets(1).Range("A1").Value
    
  If Dir(sMain) = "" Then
    MkDir sMain

'Add file Test.pdf from "C:\MISC\Test.pdf"

  End If

 
End Sub

It used to work really great before I migrated over to the 365 Microsoft platform.

I'm now getting an error on line:

VBA Code:
 If Dir(sMain) = "" Then

Can someone help me with the code?

Thanks!
Pinaceous
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
The specific error from the VBA reads:
This is a wild guess (it worked fine for me on Microsoft 365), but
VBA Code:
Sheets(1).Range("A1").Value
is a very vague way to identify the sheet. Are you sure that whatever sheet has an index of 1 in your Workbook . . . that Cell A1 in it has contents that a folder can be named? (If it's blank, this sub does nothing. It doesn't give an error. Just does nothing.)

So perhaps check the value that's in that cell in that specific sheet?

EDIT:
Does that cell have a vertical space in it? (I just tested that, and that gave an error.)
 
Upvote 0
Hi *cmowla,

I've checked the cell to make sure but it still pops an error on that line.

Is it because of the cloud & Microsoft 365?

I'm stumped!
 
Upvote 0
I have no idea. I don't mess with the cloud!
 
Upvote 0
Sorry, my workplace has the 365 but not the cloud.

Just not sure how to debug my error at this point in time.
 
Upvote 0
Hey,

I just opened up another document and tried it out and the code works.

But now, I'm curious as to why it does not work with my original workbook?

Any suggestions as to the Excel settings?

Thanks!
-pin
 
Upvote 0
Hi,​
no as it's just a question of logic not respected according to the content of variables sPath & sMain and maybe a misuse of Dir function.​
But as I'm a very beginner to guess anything …​
Another way to create a folder via MakeSureDirectoryPathExists like in this thread :​
 
Upvote 0
Hi MarcL!

I've tried out your link with the codes posted there and the
VBA Code:
 Private Declare PtrSafe Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath$) As Boolean
but I'm still getting popped an error on the drive path.

My code in its entirety is as follows:

VBA Code:
Sub CreateFolders()

    Dim FSO As Object, sourceFolder As Object, file As Object

  Dim sPath As String, sMain As String, sFolder As String
  Dim i As Long

    Set FSO = CreateObject("Scripting.FileSystemObject")
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    sMain = sPath & "\" & ThisWorkbook.Sheets(1).Range("B11").Value
     
  If Dir(sMain, vbDirectory) <> "" Then 'if the folder exists
  'Do nothing
    MsgBox "Duplicate Folders!"
   
  Exit Sub
 
  Else ' 'if the folder not exists
      MkDir sMain
        FSO.copyFile Source:="F:\MISC\1-2-STEP.WADS.SYS.DOCS SHARE DRIVE POSTED\IATS_FYS\FY4701ALS\IMPORT.pdf", Destination:=sMain & "\IMPORT.pdf"
     
        FSO.copyFile Source:="F:\MISC\1-2-STEP.WADS.SYS.DOCS SHARE DRIVE POSTED\IATS_FYS\FY4701ALS\als1.xlsx", Destination:=sMain & "\als1.xlsx"
     
        FSO.copyFile Source:="F:\MISC\1-2-STEP.WADS.SYS.DOCS SHARE DRIVE POSTED\IATS_FYS\FY4701ALS\Sending.pdf", Destination:=sMain & "\Sending.pdf"
     
        FSO.MoveFile Source:="C:\Users\ZGFFXW4\Desktop\FY 2022 WADS T-1 UTB" & Sheets(1).Range("E25").Value, Destination:=sMain & "\" & Sheets(1).Range("E25").Value

        FSO.MoveFile Source:="C:\Users\ZGFFXW4\Desktop\FY 2022 WADS T-1 UTB" & Sheets(1).Range("E31").Value, Destination:=sMain & "\" & Sheets(1).Range("E31").Value

        FSO.MoveFile Source:="C:\Users\ZGFFXW4\Desktop\FY 2022 WADS T-1 UTB" & Sheets(1).Range("E37").Value, Destination:=sMain & "\" & Sheets(1).Range("E37").Value


    End If
   
Set FSO = Nothing

 For i = 11 To 23
    sFolder = sMain & "\" & Sheets(1).Range("C" & i).Value
       
    If Dir(sFolder) = "" Then
      MkDir sFolder
    End If
  Next


End Sub

Here the code begins to create a folder and adds the files in just perfectly but it errors starting on line:

VBA Code:
     FSO.MoveFile Source:="C:\Users\ZGFFXW4\Desktop\FY 2022 WADS T-1 UTB" & Sheets(1).Range("E25").Value, Destination:=sMain & "\" & Sheets(1).Range("E25").Value

where it is suppose to then create subfolders based upon a reference a cell in my workbook.

Any suggestions??

Thanks,
Pin!
 
Upvote 0

Attachments

  • Untitled.jpg
    Untitled.jpg
    73.9 KB · Views: 4
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,971
Members
449,059
Latest member
oculus

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