Macro for creating a folder

JKMyx

New Member
Joined
Nov 9, 2016
Messages
8
Hi

I have below code for creating a folder in the same directory as the active workbook.
The folder name should be the content of the selected cells when I run the macro.

However, I get an error "Bad file name or number (Error 52)" on this code line when debugging:
VBA Code:
If Len(Dir(ThisWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then

Any idea, what the issue could be?

Here's the code I use:

VBA Code:
Sub MakeFolders()

Dim Rng As Range
Dim maxRows, maxCols, r, c As Integer
Set Rng = Selection
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
If Len(Dir(ThisWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ThisWorkbook.Path & "\" & Rng(r, c))
On Error Resume Next
End If
r = r + 1
Loop
Next c

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
A few things.

This:
VBA Code:
Dim maxRows, maxCols, r, c As Integer
only declares c as Integer. The others will be set to Variant.
You need to declare each one explicitly, i.e.
VBA Code:
Dim maxRows As Integer, maxCols As Integer, r As Integer, c As Integer


I don't think this is not a valid reference:
VBA Code:
Rng(r, c)


Your code can be simplified. No need to find the rows and columns. Simply loop through all the cells in your Selection, i.e.
VBA Code:
Sub MakeFolders()

    Dim cell As Range

    On Error Resume Next
   
    For Each cell In Selection
        If Len(Dir(ThisWorkbook.Path & "\" & cell.Value, vbDirectory)) = 0 Then
            MkDir (ThisWorkbook.Path & "\" & cell.Value)
        End If
    Next cell
   
    On Error GoTo 0
   
End Sub
 
Upvote 0
Hi Joe4,

Thanks for your answer!
I used your simplified code, it works perfectly fine, thanks.

Now I'm confronted with another issue... When I use this macro to make a folder in my One Drive, it doesn't work!
It only works if the folder is created on the local disk.

Do you have any clue to get around this problem with code?

Thanks again for your help
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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