SaveAs VBA Question

data808

Active Member
Joined
Dec 3, 2010
Messages
353
Office Version
  1. 2019
Platform
  1. Windows
I have a SaveAs VBA code that is assigned to a button. Right now it saves to a directory folder that never changes and the file name looks like this:

JS_09-12-2022.xlsm

Here is the VBA that does this:

ActiveWorkbook.SaveAs Filename:="K:\DAT\John Smith\" _
& "TYO_" & Format(Now(), "MM-DD-YYYY") & ".xlsm"

So it saves it to John Smith's folder and I do this so that the directory is static and then later at the end of the year I will have to organize the files and throw it in a 2022 folder manually within John's folder.

Is there a way so that this file can go into the current year folder based off the file name year, which it will always pull the current because of the Format(Now() function, so that I won't have to keep organizing manually or make changes to the VBA every year so that it goes into the folder I want?

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
As long as the yearly folder has already been set-up, how about:
Rich (BB code):
ActiveWorkbook.SaveAs Filename:="K:\DAT\John Smith\" _
    & Format(Now(), "YYYY") & "\" _
    & "TYO_" & Format(Now(), "MM-DD-YYYY") & ".xlsm"
 
Upvote 0
Solution
As long as the yearly folder has already been set-up, how about:
Rich (BB code):
ActiveWorkbook.SaveAs Filename:="K:\DAT\John Smith\" _
    & Format(Now(), "YYYY") & "\" _
    & "TYO_" & Format(Now(), "MM-DD-YYYY") & ".xlsm"
Excellent! Works great. I actually have another spreadsheet that does the SaveAs in a similar way but was wondering if we could assign the search of the current year folder of where to save the file and reference it to cell G2 which is where the user would enter the year (YYYY). So basically the VBA will search where to save the file based on what year the user enters.

Right now this is the current SaveAs code which uses a Dim fname as string:

fname = "C:\Users\john.smith\Desktop\CITS_" & Format(DateValue(mnth & "-01"), "mm") _
& mnth & "_" & Range("G2") & ".xlsm"

Which makes the file save like this: "CITS_12SEP_2022.xlsm"

Then concludes it with this:

ActiveWorkbook.SaveAs Filename:=fname
 
Upvote 0
Excellent! Works great. I actually have another spreadsheet that does the SaveAs in a similar way but was wondering if we could assign the search of the current year folder of where to save the file and reference it to cell G2 which is where the user would enter the year (YYYY). So basically the VBA will search where to save the file based on what year the user enters.

Right now this is the current SaveAs code which uses a Dim fname as string:

fname = "C:\Users\john.smith\Desktop\CITS_" & Format(DateValue(mnth & "-01"), "mm") _
& mnth & "_" & Range("G2") & ".xlsm"

Which makes the file save like this: "CITS_12SEP_2022.xlsm"

Then concludes it with this:

ActiveWorkbook.SaveAs Filename:=fname
Do you mean something like this?
Rich (BB code):
fname = "C:\Users\john.smith\Desktop\" & Format(Range("G2").Value,"YYYY") & "\CITS_" _
    & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("G2") & ".xlsm"
 
Upvote 0
Do you mean something like this?
Rich (BB code):
fname = "C:\Users\john.smith\Desktop\" & Format(Range("G2").Value,"YYYY") & "\CITS_" _
    & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("G2") & ".xlsm"
Thanks. Ok so I tried that code you provided and get an error message:

Run-Time error '1004':
Microsoft Excel cannot access the file
'C:\Users\john.smith\Desktop\1905\3B285D40'. There are several possible reasons:
- The file name or path does not exist.
- The file is being used by another program.
- The workbook you are trying to save has the same name as a (message gets cut off here)

Then it has the "End" or "Debug" buttons so I click Debug and it points to this line:

ActiveWorkbook.SaveAs Filename:=fname

I did a bit more testing to see if my old SaveAs line works and it does with no problems. Here is the old line:

fname = "C:\Users\john.smith\Desktop\CITS_" & Format(DateValue(mnth & "-01"), "mm") _
& mnth & "_" & Range("G2") & ".xlsm"

Sorry I also should mention that the user is not typing in the year "2022" for example, into G2 cell but rather selecting the year from a drop down list that I made with data validation. Not sure if that makes a difference. Also I'm also thinking that the formatting function may not be needed as the cell G2 is only of the year and nothing else.
 
Upvote 0
Do you mean something like this?
Rich (BB code):
fname = "C:\Users\john.smith\Desktop\" & Format(Range("G2").Value,"YYYY") & "\CITS_" _
    & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("G2") & ".xlsm"
Ok I fixed it. Here is what worked:

VBA Code:
fname = "C:\Users\john.smith\Desktop\" & Range("G2") & "\CITS_" _
    & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("G2") & ".xlsm"

Thank you so much for your help. You did all the hard work. You just opened my eyes to see the solution. Thanks again. Appreciate it.
 
Upvote 0
OK, I thought G2 was a date, but it sound like it is just a number.
Then you should just be able to use:
VBA Code:
fname = "C:\Users\john.smith\Desktop\" & Range("G2").Value & "\CITS_" _
    & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("G2") & ".xlsm"
 
Upvote 0
OK, I thought G2 was a date, but it sound like it is just a number.
Then you should just be able to use:
VBA Code:
fname = "C:\Users\john.smith\Desktop\" & Range("G2").Value & "\CITS_" _
    & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("G2") & ".xlsm"
Whoops, didn't see that you just replied again, and discovered the same thing I explained.

Glad you got it figured out!
 
Upvote 0
Whoops, didn't see that you just replied again, and discovered the same thing I explained.

Glad you got it figured out!
Thanks for your help. You showed me the way. lol
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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