Saving a File via VBA using several Excel cells

StevieMP

New Member
Joined
Sep 28, 2021
Messages
43
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi There,

I want to save a spreadsheet using VBA and the name of the file I want to use can be what has been input in several cells
e.g.
A1, (example Test)
A2, (example Test1)
A3 (example Test2)
etc...

My code is the following:

Dim Path As String
Dim filename As String

Sheets("Steve").Copy
Path = "C:\MyFolder\"
Filename = Range("A1")
ActiveWorkbook.SaveAs filename:=Path & filename & Format(Now(), "DD-MM-YYYY") & ".xlsx"


Is there a way to be able to save the file using what has been input in A1, A2 & A3 so the file saved becomes:
C:\MyFolder\Test Test1 Test2 11-03-2022.xlsx

Thank you for any help and suggestions.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try:
VBA Code:
Dim Path As String
Sheets("Steve").Copy
Path = "C:\MyFolder\"
ActiveWorkbook.SaveAs filename:=Path & Range("A1") & " " & Range("A2") & " " & Range("A3") & " " & Format(Now(), "DD-MM-YYYY") & ".xlsx"
 
Upvote 0
Thank you mumps - Is there a way to make it dynamic, so if the next time Cell A4 is populated Range("A4") is automatically added ?
 
Upvote 0
How many cells do you plan to add?
 
Upvote 0
Possibly between 1 to 6 cells, maybe at a push 7.
So, if there are only 3 inputs from cell A1 to A3 and nothing in cells A4 to A7 would that matter the latter being empty if all A1 to A7 was listed in the code?
 
Upvote 0
Assuming that there is no other data in column A other what will be used in the file name, try:
VBA Code:
Sub SaveFile()
    Dim Path As String, sName As String
    sName = Join(Application.WorksheetFunction.Transpose(Range("A1", Range("A" & Rows.Count).End(xlUp)).Value), " ")
    Sheets("Steve").Copy
    Path = "C:\MyFolder\"
    ActiveWorkbook.SaveAs filename:=Path & sName & " " & Format(Now(), "DD-MM-YYYY") & ".xlsx"
End Sub
 
Upvote 0
Solution
mumps - that has worked a treat! I cannot thank you enough.
 
Upvote 0
To coin a Frank Spencer quote :
"Every day, in every way, I'm getting better and better."

Really appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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