Excel Macro to create new folder and save file

Clanos4

New Member
Joined
May 27, 2009
Messages
9
I want to create a macro that will create a new folder called "Fungicide Quotes" under my documents and will save the workbook using the cell reference d4:f4 for the file name, which are merged cells. I have tried the following but can't get it to work. Any help would be appreciated, Thanks

Sub Save_wrkbk()

Dim strFilename, strDirname, strPathname, strDefpath As String
On Error Resume Next
strDirname = "Fungicide Quotes"

strFilename = Range("d4:f4").Value
strDefpath = "C:\My Documents\"
If IsEmpty(Filename) Then Exit Sub

MkDir strDefpath & strDirname
strPathname = strDefpath & strDirname & "\" & strFilename

ActiveWorkbook.SaveAs Filename:=strPathname, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

End sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the Board.

What error do you get and on which line? This doesn't seem to belong in your code:

If IsEmpty(Filename) Then Exit Sub
 
Upvote 0
I am not getting an error on any line, the code appears to run, but doesn't create the folder or the file. I have the workbook and the sheets protected.
 
Upvote 0
I removed that line of code, and it still doesn't work, I am not sure where to go next. Is there another way to get to the same result? Thanks
 
Upvote 0
When I remove that line I get an error,

Run-time error '76': Path not found

and it highlights the line:

MkDir strDefpath & strDirname
 
Upvote 0
Instead of:

strDefpath = "C:\My Documents\"

try:

strDefpath = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") & "\"

The path to My Documents is actually something like:

C:\Documents and Settings\user\My Documents
 
Upvote 0
Getting there the folder created and was named correctly, now I am getting a Type mismatch error

this line is highlighted

strPathname = strDefpath & strDirname & "\" & strFilename 'create total string

When I cursor over strDefpath and strDirname I get the proper responses showing but nothing over strFilename

It looks like some problem with the filename, the range d4:f4 contains text if that helps.
 
Upvote 0
I reset the range of the merge cells to just d4 instead of the range d4:f4 and it is now giving me the proper value, but that is not transferring down to the line I sent you previously. there is no value showing for the strFilename in this line.

strPathname = strDefpath & strDirname & "\" & strFilename
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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