Save to Folder Name based on cell value

kclong

Board Regular
Joined
Nov 22, 2006
Messages
80
In Excel 2007, how do I write a macro that will save to a specific folder based on a cell's value? For example, if I have folders Red, Blue, and White, and a cell where I input Blue, I want the macro to then save the file in the Blue folder. If I change the cell value to Red, the macro will then save to the Red folder, etc. Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this (change the range where folder name resides to suit):
Code:
Sub SaveToFolder()
Dim fPath As String, mPath As String, foldName As Variant
Dim filName As String, fullPath As String
'your code here
'Ready to save
filName = ThisWorkbook.Name
fPath = ThisWorkbook.Path
mPath = Left(fPath, InStrRev(fPath, "\") - 1)
foldName = Range("A1").Value   'Change range to suit
fullPath = mPath & Application.PathSeparator & foldName & Application.PathSeparator & filName
ThisWorkbook.SaveAs Filename:=fullPath
End Sub
 
Upvote 0
Thanks, but I can't seem to get it to work, but that's because I don't fully understand the code. To be more specific about my path, if I want to to go to u:\ken\excel\, and at times I may change it to a different path, such as u:\ken\projects\, how would I change the code? Thanks again for your help.
 
Upvote 0
Can you be more specific about what's not working?
The code is designed to save the workbook it resides in to a pre-existing folder whose name is in Range("A1") of the sheet that's active when the code is run. The pre-existing folder is on the same path as the workbook was on when it was opened, except for the folder if the Range("A1") value was changed after opening and prior to running the code.

If you are starting with a new workbook and/or there is no pre-existing folder, this code will not work.
 
Upvote 0
The file is currently in u:\ken\excel\. I want to change from the "excel" directory to "other" directory. When I put other in A1 and run the macro, it comes back with:

Microsoft Office Excel cannot access the file 'C:\Users\Ken\Appdata\Roaming\Microsoft\Excel\other\CED44000'.

Any thoughts?
 
Upvote 0
The code I posted would place the file in u:\ken\excel\other if a folder named 'other' exists in that directory.
 
Upvote 0
The directory u:\ken\excel\other exists, but the message says it wants to save it to:

'C:\Users\Ken\Appdata\Roaming\Microsoft\Excel\other\CED44000'

Why is it putting CED44000 at the end? Could that be the problem?
 
Upvote 0
Actually the problem seems to be that the code is changing u:\ken\excel\ to C:\Users\Ken\Appdata\Roaming\Microsoft\Excel\. Any idea how to change that?
 
Upvote 0
In any empty cell in the workbook you are running the code from enter this formula:
Code:
=CELL("filename",A1)
and post what the cell returns.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,445
Members
452,915
Latest member
hannnahheileen

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