run time error 52 ("Bad file name or number")

Stan70

New Member
Joined
Mar 11, 2022
Messages
2
Platform
  1. Windows
I attempting to run vba code to create a directory from a cell.
This code worked when last run. Now, when I attempt to run it, I keep getting run time error 52 ("Bad file name or number").

there is no network failure.
The target directory does exist, and I have write access.
No "," or "()/:" in te selected cel

Any suggestions?????



CODE:

Sub Create_Folders()

BrowseForFolder = "N:\PR\Planning"
Dim Rng As Range
Dim maxRows, maxCols, r, c As Integer
Set Rng = Selection
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count

Sheets("Create dir").Select
Range("B6").Select

For c = 1 To maxCols
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then <--- this line is markt if i look at Debug
MkDir (BrowseForFolder & "\" & Rng(r, c))

On Error Resume Next
End If
r = r + 1
Loop

Next c

Sheets("Overzicht").Select
MsgBox " " & BrowseForFolder & Rng(r, c), vbInformation, "Created Directory:" & Rng(r, c)

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,
try replacing that line with this & see if resolves your issue

Rich (BB code):
If Len(Dir(ActiveWorkbook.Path & "\" & Rng.Cells(r, c), vbDirectory)) = 0 Then 

Also as an aside, you variable declaration

VBA Code:
Dim maxRows, maxCols, r, c As Integer

only the last variable is of the specified data type all others are variants.
In VBA you have to explicitly declare data type for each variable even if on same line.

VBA Code:
Dim maxRows As Long, maxCols As Long, r As Long, c As Long

Note also that I changed variables to Long Integers as when working with ranges you can exceed 32,767 rows.

Dave
 
Upvote 0
Thanks for the input Dave,
Only no succes still the same error.

Strange it have always worked well, and now not any more?????
I think it's is not a fault in the script, but a problem of Excel ??

The only thing i need is to press a button (macro) and make a directory of the selected cell (always the same cell, different cell value)
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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