VBA to save with a name based on three cell values, new file type, and user chooses location prompt

ChrisMac1

New Member
Joined
Jul 15, 2024
Messages
32
Office Version
  1. 365
Platform
  1. Windows
HI there,

I have a workbook that, as the final code to run, I want the user to receive the save as prompt, but the filename to be made up of the values in three cells of a hidden sheet.

All the user needs to choose is a location and hit save.

Any thoughts on an easy way to do this please?

Many thanks,
Chris

BTW, I am in the UK so if I don't respond immediately be mindful of my local time please.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try ths:
VBA Code:
Sub save_based_three_cells()
  Dim cell1 As String, cell2 As String, cell3 As String
  Dim sFile As String
  
  With Sheets("Sheet5")           'Fit the name of your hidden sheet
    cell1 = .Range("B10").Value   'Fit cell1
    cell2 = .Range("C10").Value   'Fit cell2
    cell3 = .Range("D10").Value   'Fit cell3
  End With
  
  With Application.FileDialog(msoFileDialogSaveAs)
    .Title = "File Save As"
    .AllowMultiSelect = False
    .InitialFileName = cell1 & "_" & cell2 & "_" & cell3
    .FilterIndex = 1
    If .Show Then
      sFile = .SelectedItems(1)
      ActiveWorkbook.SaveAs Filename:=sFile, FileFormat:=xlOpenXMLWorkbook
    End If
  End With
End Sub


NOTE: If any of the cells have a date, then use the following, for example:
VBA Code:
cell3 = Format(.Range("D10").Value, "YYYY-MM-DD")

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Solution
Try ths:
VBA Code:
Sub save_based_three_cells()
  Dim cell1 As String, cell2 As String, cell3 As String
  Dim sFile As String
 
  With Sheets("Sheet5")           'Fit the name of your hidden sheet
    cell1 = .Range("B10").Value   'Fit cell1
    cell2 = .Range("C10").Value   'Fit cell2
    cell3 = .Range("D10").Value   'Fit cell3
  End With
 
  With Application.FileDialog(msoFileDialogSaveAs)
    .Title = "File Save As"
    .AllowMultiSelect = False
    .InitialFileName = cell1 & "_" & cell2 & "_" & cell3
    .FilterIndex = 1
    If .Show Then
      sFile = .SelectedItems(1)
      ActiveWorkbook.SaveAs Filename:=sFile, FileFormat:=xlOpenXMLWorkbook
    End If
  End With
End Sub


NOTE: If any of the cells have a date, then use the following, for example:
VBA Code:
cell3 = Format(.Range("D10").Value, "YYYY-MM-DD")

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
DanteAmor, you're an absolute legend, thank so much for the speedy response, worked a treat... Thanks so much!!
 
Upvote 0
Try ths:
VBA Code:
Sub save_based_three_cells()
  Dim cell1 As String, cell2 As String, cell3 As String
  Dim sFile As String
 
  With Sheets("Sheet5")           'Fit the name of your hidden sheet
    cell1 = .Range("B10").Value   'Fit cell1
    cell2 = .Range("C10").Value   'Fit cell2
    cell3 = .Range("D10").Value   'Fit cell3
  End With
 
  With Application.FileDialog(msoFileDialogSaveAs)
    .Title = "File Save As"
    .AllowMultiSelect = False
    .InitialFileName = cell1 & "_" & cell2 & "_" & cell3
    .FilterIndex = 1
    If .Show Then
      sFile = .SelectedItems(1)
      ActiveWorkbook.SaveAs Filename:=sFile, FileFormat:=xlOpenXMLWorkbook
    End If
  End With
End Sub


NOTE: If any of the cells have a date, then use the following, for example:
VBA Code:
cell3 = Format(.Range("D10").Value, "YYYY-MM-DD")

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Sorry DanteAmor, one thing, I need to keep the filetype as macro enabled, sorry, my mistake. I tried adding this but it didn't work...

ActiveWorkbook.SaveAs Filename:=sFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled
 
Upvote 0
Change this:
VBA Code:
.FilterIndex = 2

VBA Code:
ActiveWorkbook.SaveAs Filename:=sFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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