Create new folder based on cell values

zovche

Board Regular
Joined
Mar 21, 2013
Messages
125
Hello,

I would like to create new folder with folder name based on cells in excel (like Range("M1") & "_" & Range("D2")).

Example path:

"XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
Where:
M1=123456
D2=Test
so I get folder "XA\DT\1. PROTEH\123456_Test"

If folder exists it would be great to skip and continue with rest of the code, I would like to avoid overwriting if folder exists and to avoid break message stopping the code.

Thanks.

Thank you.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hello,

I would like to create new folder with folder name based on cells in excel (like Range("M1") & "_" & Range("D2")).

Example path:

"XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
Where:
M1=123456
D2=Test
so I get folder "XA\DT\1. PROTEH\123456_Test"

If folder exists it would be great to skip and continue with rest of the code, I would like to avoid overwriting if folder exists and to avoid break message stopping the code.

Thanks.

Thank you.
Hi zovche,

Is this any good to you?

Code:
Sub CreateFolder()
Dim NewFolder As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
           MkDir NewFolder
        Else
            MsgBox "Folder already exists"
        End If
End Sub
 
Upvote 0
Hi zovche,

Is this any good to you?

Code:
Sub CreateFolder()
Dim NewFolder As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
           MkDir NewFolder
        Else
            MsgBox "Folder already exists"
        End If
End Sub

Hi Fishboy,

That's just perfect. I tested multiple combinations and works for any just like I needed.

Thank you very much, I googled some solutions but they were really complicated, I was like it must be something smoother and less complex, so I came here. Thank you once again.

Vlatko
 
Upvote 0
Hi Fishboy,

That's just perfect. I tested multiple combinations and works for any just like I needed.

Thank you very much, I googled some solutions but they were really complicated, I was like it must be something smoother and less complex, so I came here. Thank you once again.

Vlatko
Happy to help mate. Thanks for the positive feedback :)
 
Upvote 0
Happy to help mate. Thanks for the positive feedback :)

My pleasure ;)
I just come to one more additional thing related to this :)
In next step I have another macro where now I would like to save worksheet into that new path.

I tried:
Code:
ActiveWorkbook.SaveAs "XA\DT\1. PROTEH\" & Range("M1").Text & "_" & Range("D2").Text & "\" & ActiveWorkbook.Name

And:
Code:
NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
ActiveWorkbook.SaveAs NewFolder & ActiveWorkbook.Name

But didn't work.
 
Upvote 0
How about this?

Rich (BB code):
Sub SavingWorkbook()
Dim NewFolder As String, SaveName As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        SaveName = ActiveWorkbook.Name & ".xlsm"
            ActiveWorkbook.SaveAs NewFolder  & "\" & SaveName
End Sub

Or you could try combining it all into one macro. If the folder doesn't exist, create it then save the file. If the folder already exists, just save the file.

Rich (BB code):
Sub BothInOne()
Dim NewFolder As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
            MkDir NewFolder
                SaveName = ActiveWorkbook.Name & ".xlsm"
                    ActiveWorkbook.SaveAs NewFolder & "\" & SaveName
        Else
                SaveName = ActiveWorkbook.Name & ".xlsm"
                    ActiveWorkbook.SaveAs NewFolder & "\" & SaveName
        End If
End Sub
 
Upvote 0
How about this?

Rich (BB code):
Sub SavingWorkbook()
Dim NewFolder As String, SaveName As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        SaveName = ActiveWorkbook.Name & ".xlsm"
            ActiveWorkbook.SaveAs NewFolder  & "\" & SaveName
End Sub

Or you could try combining it all into one macro. If the folder doesn't exist, create it then save the file. If the folder already exists, just save the file.

Rich (BB code):
Sub BothInOne()
Dim NewFolder As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
            MkDir NewFolder
                SaveName = ActiveWorkbook.Name & ".xlsm"
                    ActiveWorkbook.SaveAs NewFolder & "\" & SaveName
        Else
                SaveName = ActiveWorkbook.Name & ".xlsm"
                    ActiveWorkbook.SaveAs NewFolder & "\" & SaveName
        End If
End Sub

I tried it but I get same error message:
"Run-time error '1004'
Method 'SaveAs' of object '_Workbook' failed"

On debug mode it marks me this line
Code:
ActiveWorkbook.SaveAs NewFolder & "\" & SaveName
 
Upvote 0
Hmm, strange as both of my suggestions work without issue at this end. Do you have any other workbooks open? If so, are they what Excel would consider the "Active Workbook" instead of the one with the macros in? Are you trying to save as a non-macro enabled workbook instead of a .xlsm?
 
Upvote 0
How about this?

Rich (BB code):
Sub SavingWorkbook()
Dim NewFolder As String, SaveName As String
    NewFolder = "XA\DT\1. PROTEH\" & Range("M1") & "_" & Range("D2")
        SaveName = ActiveWorkbook.Name & ".xlsm"
            ActiveWorkbook.SaveAs NewFolder  & "\" & SaveName
End Sub

Ups sorry, I made dummy mistake I miss-type Sheet name from where I take the cell value grrrr at me
Everything's fine now works like a charm any example above.

Code:
Worksheets("***").Range("M1") & "_" & Worksheets("***").Range("D2")

Part with asterisks *** was typed wrongly, I was in another sheet and whole the time looking on different name.

Case closed, thanks much for this. Have a great day.
 
Last edited:
Upvote 0
Ups sorry, I made dummy mistake I miss-type Sheet name from where I take the cell value grrrr at me
Everything's fine now works like a charm any example above.

Code:
Worksheets("***").Range("M1") & "_" & Worksheets("***").Range("D2")

Part with asterisks *** was typed wrongly, I was in another sheet and whole the time looking on different name.

Case closed, thanks much for this. Have a great day.
No problem, sometimes simple little mistakes are easily done :)

Happy to hear you got it all working.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,428
Members
448,896
Latest member
MadMarty

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