Trying to add a line to vba

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
708
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Morning all.
I am trying to add this line to the script in order for the "SaveAs" function to rename the file as the text in the quotes, and the cell valve. Thanks all.

Code:
 "Req-" & Range.("P3")

This is what I currently have. This is not my VB, I forgot where I copied it from.

Code:
Sub Save_As()
Dim file_name As Variant

    ' Get the file name.
    file_name = Application.GetSaveAsFilename( _
        FileFilter:="Excel Files,*.xls,All Files,*.*", _
        Title:="Save As File Name")

    ' See if the user canceled.
    If file_name = False Then Exit Sub

    ' Save the file with the new name.
    If LCase$(Right$(file_name, 4)) <> ".xls" Then
        file_name = file_name & ".xls"
    End If
    ActiveWorkbook.SaveAs Filename:=file_name

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The only issue I have with that is there is no Save as UI. The user does not have the option to cancel.
Can this be amended so that the user can see the save as UI?
 
Upvote 0
Try this

Code:
Sub Save_As()
Dim file_name As String

' Get the file name.
file_name = "Req-" & Range("P3").Value
If LCase$(Right$(file_name, 4)) <> ".xls" Then
    file_name = file_name & ".xls"
End If
If MsgBox("Are you sure you want to save as " & file_name, vbQuestion + vbYesNo) = vbNo Then Exit Sub
ActiveWorkbook.SaveAs Filename:=file_name

End Sub
 
Upvote 0
Yes, that does work. The procedure defaulted to the "My Documents" folder. With the SaveAs UI, the user can change the directory and cancel.
Your vba works correct, but I need the functionality of the save as UI.
Thanks again.
Pujo
 
Upvote 0
Try this - GetFolder function courtesy of Richard Schollar

Code:
Sub Save_As()
Dim file_name As String

' Get the file name.
file_name = "Req-" & Range("P3").Value
If LCase$(Right$(file_name, 4)) <> ".xls" Then
    file_name = file_name & ".xls"
End If
file_name = GetFolder("") & "\" & file_name
If MsgBox("Are you sure you want to save as " & file_name, vbQuestion + vbYesNo) = vbNo Then Exit Sub
ActiveWorkbook.SaveAs Filename:=file_name

End Sub

Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
 
Upvote 0
Now that is nice....
It seems that when the UI is open, it is not showing the file name, it is blank.
Also, will the UI open to the previous saved directory, or is the default "My Documents"?
 
Upvote 0
This should help with that but I'm not sure that we can enhance this any further

Rich (BB code):
file_name = GetFolder(CurDir) & "\" & file_name
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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