Save File to Folder Based on Cell Value

BluHound

New Member
Joined
Jul 26, 2017
Messages
3
Hi Folks,

So I have two Subs that I would like to work together. One to create a new folder, if it doesn't already exist and the second to SaveAs with the file name based on cell values.

I would like for the code to

1) create the folder (if needed)
2) SaveAs with new file name (based on cell values)
3) Place new file in the folder created.

So far I have items 1 and 2 working independently, but the moment have yet to be able to get the workbook to save to the folder created with Sub IfNewFolder.

If anyone can help, i'd greatly appreciate it. I've been scouring the internet for almost a month and have now been given a dead line (if i can't get it to work, we'll just go back to the old way. doable, but deplorable ;) )



Code:
Sub IfNewFolder()

Dim part1 As String
Dim part3 As String
Dim FolderCreate As String

part1 = Range("E4").Value
part3 = Range("C10").Value

If Len(Dir("O:\Human Capital\Training\Client Facing Training\Customers\ " & part3, vbDirectory)) = 0 Then
   MkDir "O:\Human Capital\Training\Client Facing Training\Customers" & part3
End If

End Sub

Code:
Sub SaveCustomizedCourse()
     
Dim part1 As String
Dim part3 As String


part1 = Range("E4").Value 'Quote Number
part3 = Range("C10").Value 'Company Name


ChDir "O:\Human Capital\Training\Client Facing Training\Customers\" ' From what I've read on the internet, this is telling excel to save files to this directory... 

' Creates file to directory Customers. But I can't get it to recognize the new folder created in the sub above...

ActiveWorkbook.SaveAs Filename:= _
"O:\Human Capital\Training\Client Facing Training\Customers" & part1 & "_" & part3 & "_Custom.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


End Sub

Thank you,

Blu
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Please try the following code on a copy of your workbook.
Comments on changes are in the code

Code:
Option Explicit
'----- by declaring the base path here _
       it's easier to change and _
       code is easier to read and less prone to typos _
 -----
Const MYPATH As String = "O:\Human Capital\Training\Client Facing Training\Customers\"

Sub IfNewFolder()

'Dim part1 As String 'this variable is not used -----
Dim part3 As String
'----- Dim FolderCreate As String  'this variable is not used -----

'----- part1 = Range("E4").Value 'not used here -----
part3 = Range("C10").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 'Quote Number
part3 = Range("C10").Value 'Company Name

IfNewFolder 'create company subfolder

'ChDir MYPATH ' From what I've read on the internet, this is telling excel to save files to this directory...
'-----you don't have to do that because this path is included in the filename in the SaveAs below -----

' Creates file to directory Customers. But I can't get it to recognize the new folder created in the sub above...

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

'----- instead put company in path (point to company subfolder) -----
ActiveWorkbook.SaveAs Filename:= _
MYPATH & part3 & "\" & part1 & "_Custom.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub
 
Upvote 0
@ask2tsp Thank you so much! This works great. Though I'm not exactly sure how it functions (still learning), it does work and i've been having great results. Thank you for taking the time to help me with this.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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