Make Folder and Sub

jaime1182

New Member
Joined
Dec 11, 2007
Messages
49
Office Version
  1. 2013
Platform
  1. Windows
Hi all

Hope you are all well.

I am a bit stuck with a macro and was hoping someone could help.

I have the following table starting at Column D:

EntityProject AProject BProject CProject D
AppleChairTableCabinetRack
BananaCupboardLamp
ChocolateDeskOttomanDrawer
Date PlumsTable Lamp

I need to create Folders for the entities and nest the project subfolders within.

I found this code and it works - for the first row.

Code:
Sub CreateFolder()

Dim FPath As String, newDir As String
Dim cell As Range, col As Range, rngFolder As Range, rngCol As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set rngFolder = ws.Range("D2", ws.Cells(Rows.Count, "D").End(xlUp))

FPath = "C:\Users\Admin\Dropbox\"
For Each cell In rngFolder
    newDir = FPath & cell & "\"
    MkDir newDir
    Set rngCol = ws.Range("E" & cell.Row, ws.Cells(cell.Row, Columns.Count).End(xlToLeft))
    For Each col In rngCol
        MkDir newDir & col
    Next
Next

End Sub

When I get to the second MkDir, I get Runtime error '75' : Path/File access error.

Code:
    For Each col In rngCol
-->        MkDir newDir & col
    Next

I can see that all the subfolders for row 1 is made so I am presuming it's hitting an error getting to row 2.

How do I get it to loop back up and then make the next folder and subfolders?

Much thanks!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:

VBA Code:
Sub CreateFolder()
  Dim ws As Worksheet
  Dim FPath As String, newDir As String
  Dim cell As Range, col As Range, rngFolder As Range, rngCol As Range
  
  Set ws = ActiveWorkbook.Sheets("Sheet1")
  Set rngFolder = ws.Range("D2", ws.Cells(Rows.Count, "D").End(xlUp))
  
  FPath = "C:\Users\Admin\Dropbox\"
  
  For Each cell In rngFolder
    newDir = FPath & cell.Value & "\"
    If Dir(newDir, vbDirectory) = "" Then
      MkDir newDir
    End If
    Set rngCol = ws.Range("E" & cell.Row, ws.Cells(cell.Row, Columns.Count).End(xlToLeft))
    For Each col In rngCol
      If col.Value <> "" Then
        If Dir(newDir & col.Value, vbDirectory) = "" Then
          MkDir newDir & col.Value
        End If
      End If
    Next
  Next
End Sub
 
Upvote 0
Solution
You are an absolute genius!!!

Much thanks! Now to try and get the copy macro working! Thanks so much! Saved my bacon again!
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

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