Trying to MkDir, ActiveWorkbook.SaveAs using cell variables -and failing. What is IfNewFolder?

GaryG210

New Member
Joined
Nov 2, 2017
Messages
4
I really could use some advice. I am new to this forum, so please forgive my mistakes.
Thanks, in advance.

I wish to have a button launched macro that saves my workbook to a new folder -both of which are named after cells on the active worksheet.
So far, I can get the folder created (from "E4"), and then the file created (from both "E4" and "E7") - but the saved file ends up outside of that folder.
I would like it to be saved IN that folder, if possible.

Any help would be greatly appreciated. Thanks again!

This is what I have:

Sub UploadFileTo_G()


Application.DisplayAlerts = False

Dim SaveName As String
SaveName = ActiveSheet.Range("E4").Value

MkDir "G:\Service\SN INSPECTIONS" & SaveName

Dim FileName As String
FileName = ActiveSheet.Range("E4").Value & "_" & Range("E7")

'trying to place the named file into the created folder...
ActiveWorkbook.SaveAs FileName:="G:\Service\SN INSPECTIONS" & FileName & ".xlsm"


(but of course, it appears OUTSIDE of the folder...)


I found (something like) this on this forum, which addresses my kind of problem... but unfortunately, I had a very similar result:

Option Explicit


Const MYPATH As String = "G:\Service\SN INSPECTIONS"


Sub IfNewFolder()


Dim part3 As String
part3 = Range("E4").Value


If Len(Dir(MYPATH & part3, vbDirectory)) = 0 Then
MkDir MYPATH & part3
End If




End Sub
Sub SaveCustomizedCourse()

Dim part1 As String
Dim part3 As String


part1 = Range("E4").Value 'ORDER
part3 = Range("E7").Value 'SERIAL


IfNewFolder


ActiveWorkbook.SaveAs FileName:= _
MYPATH & part1 & " " & part3 & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


End Sub
 

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.
Try
Code:
ActiveWorkbook.SaveAs Filename:="G:\Service\SN INSPECTIONS" & SaveName & "\" & Filename & ".xlsm"
 
Upvote 0
Try
Code:
ActiveWorkbook.SaveAs Filename:="G:\Service\SN INSPECTIONS" & SaveName & "\" & Filename & ".xlsm"

Thank you Sir Fluff, I sincerely appreciate your suggestion.
I may have mis-handled it, but unfortunately the substituted code you recommended resulted in a file named "SN INSPECTIONSE4E4_E7" saved to the "Service" folder (instead of the desired "E4_E7" file name saved to the SN INSPECTIONS folder.
Be that as it may, I was able to succeed in this effort with a code given to me by an associate. Honestly, I do not understand how or why it works when so many other attempts did not. But, alas, I will just do what I imagine so many others do and simply be thankful to the CODE GODS that mercifully allow us to ultimately proceed with our lives.
Thanks again, sincerely.
GaryG210


Oh, the code that ultimately worked (for me) was:

Sub SaveWorksheetPerBrian()
Dim saveAsFilename, saveAsDirectory, Errordescription As String
'On Error GoTo fileSaveError 'If it errors, go to the error handler
saveAsFilename = ActiveSheet.Range("E4").Value & "_" & Range("E7")
saveAsDirectory = "\\il72w0017.global.ds.mycompany.com\hlspa1w\Service\SN INSPECTIONS" & ActiveSheet.Range("E4").Value
If Dir(saveAsDirectory, vbDirectory) = vbNullString Then 'Check to see if the directory is created, if not create it
Errordescription = "There was an error creating the directory. "
MkDir (saveAsDirectory)
End If
'Errordescription = "There was an error writing the file. "
ThisWorkbook.SaveAs (saveAsDirectory & "" & saveAsFilename) 'Save the file
MsgBox "File successfully copied to W:Drive!"
End Sub

--"On Error" gave me a problem...so I killed it, along with "Errordescription"... ;-)
 
Upvote 0
btw Fluff, please disregard my previous "Sir Fluff" address, as I now realize this could have been improper :-)
 
Upvote 0
btw Fluff, please disregard my previous "Sir Fluff" address, as I now realize this could have been improper :-)
:confused: What previous address?, the above is only your 2nd post. ;)
 
Upvote 0
I'm so sorry Fluff, the timeline of these posts look correct on my end, right now. Hmm.
I guess that (for you) my third post for Today (at 04:27PM) apologizing for my second post for Today (at 04:03PM) didn't actually post third, but instead it posted second...and for that I guess I will apologize, too. :-) Oh, and don't forget DST this Sunday...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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