Button to save and name the file based on cell value

data808

Active Member
Joined
Dec 3, 2010
Messages
353
Office Version
  1. 2019
Platform
  1. Windows
I have a cell A2 that will have the month entered (JANUARY for example). Then C2 will have the year entered (2022 for example). I have a button that I want to assign a macro to do this for me. If possible, when the macro names the file I would like it to be in this format "ECITATIONS_01JAN_2022.xlsm" for the month of January. February would be "ECITATIONS_02FEB_2022.xlsm" and so on. Right now I have a VBA code that saves and names the file but not based on the value of cells. It just names it to what is in the code and also plugs in the current date.

ActiveWorkbook.SaveAs Filename:="C:\Users\Desktop\" _
& "ECITATIONS_" & Format(Now(), "MM-DD-YYYY") & ".xlsm"

Any help is appreciated. Thanks.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
This worked for me, you'll have to change some stuff based on active sheet but i followed your methodology for where the date fields are

A2 = the month
B2 = The day
C2 = The year

Option Explicit

Sub SavetheDate()
Dim strMonth As String, Day As Long, Year As Long

With ActiveSheet
strMonth = .Cells(2, 1)
Day = .Cells(2, 2)
Year = .Cells(2, 3)
End With

Debug.Print "ECITATIONS_" & Day & strMonth & Year & ".xlsm"
ActiveWorkbook.SaveAs Filename:="C:\Users\Desktop\" & "ECITATIONS_" & Day & strMonth & Year & ".xlsm"

End Sub
 
Upvote 0
If I understand you correctly, I think this is all you need:
VBA Code:
    Dim fname As String
    fname = "C:\Users\Desktop\ECITATIONS_01" & Left(Range("A2"), 3) & "_" & Range("C2") & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=fname
 
Upvote 0
If I understand you correctly, I think this is all you need:
VBA Code:
    Dim fname As String
    fname = "C:\Users\Desktop\ECITATIONS_01" & Left(Range("A2"), 3) & "_" & Range("C2") & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=fname
Thank you for the help. It's just about correct. I just need the numbers in from of "ECITATIONS" to follow the month. Right now its static to 01 which would be for only January.
 
Upvote 0
Thank you for the help. It's just about correct. I just need the numbers in from of "ECITATIONS" to follow the month. Right now its static to 01 which would be for only January.
Your original code seemed to suggest a day piece. (Format(Now(), "MM-DD-YYYY")) I was assuming that that you wanted the first day, since you did not seem to have a cell for the day.

So, you want the number AND the three letter abbreviation to be for the month, so you would have:
01JAN
02FEB
03MAR
04APR
...


Is that correct?

If so, then try this:
VBA Code:
    Dim mnth As String
    Dim fname As String
    mnth = Left(Range("A2"), 3)
    fname = "C:\Users\Desktop\ECITATIONS_" & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("C2") & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=fname
 
Upvote 0
Solution
Your original code seemed to suggest a day piece. (Format(Now(), "MM-DD-YYYY")) I was assuming that that you wanted the first day, since you did not seem to have a cell for the day.

So, you want the number AND the three letter abbreviation to be for the month, so you would have:
01JAN
02FEB
03MAR
04APR
...


Is that correct?

If so, then try this:
VBA Code:
    Dim mnth As String
    Dim fname As String
    mnth = Left(Range("A2"), 3)
    fname = "C:\Users\Desktop\ECITATIONS_" & Format(DateValue(mnth & "-01"), "mm") & mnth & "_" & Range("C2") & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=fname
Brilliant! That is exactly what I wanted. Thank you so much for the help. The 01, 02, 03, etc... was to represent the number month and to help with sorting when the files sit in a folder. I don't like when it sorts alphabetically because then the months are all out of order. Thanks again.
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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