Button to name file and save using VBA

data808

Active Member
Joined
Dec 3, 2010
Messages
353
Office Version
  1. 2019
Platform
  1. Windows
I originally have this code to save name and save a file when I click on a control button and it works great. Here is the code:

VBA Code:
If [C2] = "" Or [G2] = "" Then
MsgBox "Please Select The Month And Year From Drop Down List." & vbNewLine & _
"Then Click The Create File Button."
Exit Sub
End If
Dim mnth As String
Dim fname As String
mnth = Left(Range("C2"), 3)
fname = "C:\Users\JD\Desktop\ECITATIONS_" & Format(DateValue(mnth & "-01"), "mm") _
& mnth & "_" & Range("G2") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=fname

So then I tried creating another one but get an error message. The error says:
Compile error in hidden module: Module2. This error commonly occurs when code is incompatible with the version, platform, or architecture of this application. Click "Help" for information on how to correct this error.

Here is the code I have so far:

Sub Auto_Save_File()

If [B2] = "" Then
MsgBox "Please Enter Monday's Date In This Format MM/DD/YY." & vbNewLine & _
"Then Click Auto Save Button."
[B2].Select
Exit Sub
End If
Dim yr As String
Dim mnth As String
Dim dd1 As String
Dim dd2 As String
Dim fname As String
yr = Right(Range("B2"), 4)
mnth = Left(Range("B2"), 2)
dd1 = Left(Range("B2"), 3, 2)
dd2 = Left(Range("Z2"), 3, 2)

fname = "C:\Users\JD\Desktop\" & Format(DateValue & yr & "_") _
& mnth & "_" & dd1 & "-" & dd2 & ".xlsm"
ActiveWorkbook.SaveAs Filename:=fname

End Sub

This is coded for a control button I created. User types in dates into cells B2 and Z2 in this format mm/dd/yyyy. I would like the file to save as "2022_09_05-09.xlsm" if the user types 09/05/2022 in cell B2 and 09/09/2022 in cell Z2. Basically naming the file for this week from Monday 05 through Friday 09. I didn't understand how capture dd1 and dd2 strings so the ranges might be off. Can someone please assist or correct this code? Thanks for the help.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I originally have this code to save name and save a file when I click on a control button and it works great. Here is the code:

VBA Code:
If [C2] = "" Or [G2] = "" Then
MsgBox "Please Select The Month And Year From Drop Down List." & vbNewLine & _
"Then Click The Create File Button."
Exit Sub
End If
Dim mnth As String
Dim fname As String
mnth = Left(Range("C2"), 3)
fname = "C:\Users\JD\Desktop\ECITATIONS_" & Format(DateValue(mnth & "-01"), "mm") _
& mnth & "_" & Range("G2") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=fname

So then I tried creating another one but get an error message. The error says:
Compile error in hidden module: Module2. This error commonly occurs when code is incompatible with the version, platform, or architecture of this application. Click "Help" for information on how to correct this error.

Here is the code I have so far:

Sub Auto_Save_File()

If [B2] = "" Then
MsgBox "Please Enter Monday's Date In This Format MM/DD/YY." & vbNewLine & _
"Then Click Auto Save Button."
[B2].Select
Exit Sub
End If
Dim yr As String
Dim mnth As String
Dim dd1 As String
Dim dd2 As String
Dim fname As String
yr = Right(Range("B2"), 4)
mnth = Left(Range("B2"), 2)
dd1 = Left(Range("B2"), 3, 2)
dd2 = Left(Range("Z2"), 3, 2)

fname = "C:\Users\JD\Desktop\" & Format(DateValue & yr & "_") _
& mnth & "_" & dd1 & "-" & dd2 & ".xlsm"
ActiveWorkbook.SaveAs Filename:=fname

End Sub

This is coded for a control button I created. User types in dates into cells B2 and Z2 in this format mm/dd/yyyy. I would like the file to save as "2022_09_05-09.xlsm" if the user types 09/05/2022 in cell B2 and 09/09/2022 in cell Z2. Basically naming the file for this week from Monday 05 through Friday 09. I didn't understand how capture dd1 and dd2 strings so the ranges might be off. Can someone please assist or correct this code? Thanks for the help.
Ok I think I'm a little closer now. Here is the code I'm using but still getting an error message:

VBA Code:
Sub Auto_Save_File()

If [B2] = "" Then
    MsgBox "Please Enter Monday's Date In This Format MM/DD/YY." & vbNewLine & _
    "Then Click Auto Save Button."
    [B2].Select
    Exit Sub
End If
Dim yr As String
Dim mnth As String
Dim dd1 As String
Dim dd2 As String
Dim fname As String
yr = Right(Range("B2"), 4)
mnth = Left(Range("B2"), 2)
dd1 = Mid(Range("B2"), 4, 2)
dd2 = Mid(Range("Z2"), 4, 2)

fname = "C:\Users\JD\Desktop\" & yr & "_" _
& mnth & "_" & dd1 & "-" & dd2 & ".xlsm"

ActiveWorkbook.SaveAs Filename:=fname

End Sub


The error message I get says:
Run-time error '1004':
Microsoft Excel cannot access the file
'C:\Users\JD\Desktop\2022_9\_\2-\E75CEA30'. 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

It gets cut off. I suspect that my Mid functions are off with dd1 and dd2 because the function bar is not reflecting what is in the cell. For example, if I type "09/05/2022" I formatted the cell to show this format, however, in the function bar the 0's are deleted and it will show "9/5/2022" in the function bar. So would that affect the Mid functions for dd1 and dd2?
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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