Save as with location pulling username itself

jr0124

New Member
Joined
Dec 28, 2016
Messages
16
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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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
 
Upvote 0
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!
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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