Save as with location pulling username itself

jr0124

New Member
Joined
Dec 28, 2016
Messages
12
Hello excel help folks,

I am looking to write up something that pops up the save as dialog box, pulls the user name to be able to add to the path as to where the file will be saving. I was able to figure out the code to get it to work how I want on my computer specificialy but would like that path to pull in the current username so it can work on any computer for any user.

This is the code that I was able to get to work thus far.

VBA Code:
Sub saveaswithlocation()
 
Dim file_name As Variant
Dim fName As String
 
ChDrive "C"
ChDir "C:\Users\[B][I]username[/I][/B]\Documents\Job Folders\" & Range("C2")
 
fName = Range("C2").Value & " - Offer Checklist - " & Range("C15").Value & " " & Range("C16").Value
 
 
' Get the file name.
file_name = Application.GetSaveAsFilename(fName, _
Filefilter:="Excel Macro-Enabled Workbook,*.xlsm,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)) <> ".xlsm" Then
file_name = file_name & ".xlsm"
End If
ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=52
End Sub
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,973
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Do you mean the below?

Rich (BB code):
Sub saveaswithlocation()
 
    Dim file_name As Variant
    Dim fName As String
 
    ChDrive "C"
    ChDir "C:\Users\" & Environ("username") & "\Documents\Job Folders\" & Range("C2")
 
    fName = Range("C2").Value & " - Offer Checklist - " & Range("C15").Value & " " & Range("C16").Value
 
 
    ' Get the file name.
    file_name = Application.GetSaveAsFilename(fName, _
                                              Filefilter:="Excel Macro-Enabled Workbook,*.xlsm,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, 5)) <> ".xlsm" Then
        file_name = file_name & ".xlsm"
    End If
    ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=52
End Sub
 

jr0124

New Member
Joined
Dec 28, 2016
Messages
12
Do you mean the below?

Rich (BB code):
Sub saveaswithlocation()

    Dim file_name As Variant
    Dim fName As String

    ChDrive "C"
    ChDir "C:\Users\" & Environ("username") & "\Documents\Job Folders\" & Range("C2")

    fName = Range("C2").Value & " - Offer Checklist - " & Range("C15").Value & " " & Range("C16").Value


    ' Get the file name.
    file_name = Application.GetSaveAsFilename(fName, _
                                              Filefilter:="Excel Macro-Enabled Workbook,*.xlsm,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, 5)) <> ".xlsm" Then
        file_name = file_name & ".xlsm"
    End If
    ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=52
End Sub

That was it. Thank you!
 

Watch MrExcel Video

Forum statistics

Threads
1,127,199
Messages
5,623,315
Members
415,966
Latest member
ctorohuamanchumo

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
Top