Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vba excel 2010 Environ("username") not working in saveas filepath

Tags:

vba

excel-2010

For some reason I can get the Message Box to tell me what the username is, but it doesn't work the same way within the save as path. I get a "Cannot find path" error.

Not sure how to fix this. I've also looked at references, and they seem like they are correct.

If I change the path to include the actual username per computer, the macro works fine. But I need this to be a global macro for any computer/user. Any help would be greatly appreciated.

Here is what I have:

Sub SAVEAS_2010()
'
' SAVEAS_2010 Macro
MsgBox Environ("username")

Dim UserName As String
UserName = Environ("username")

ChDir "C:\Users\" & UserName & "\Dropbox\Open Machine Schedule"
ActiveWorkbook.SaveAs FileName:= _
    "C:\Users\" & UserName & "\Dropbox\Open Machine Schedule\Open Machine Schedule - Current_2.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
like image 493
cheapkid1 Avatar asked Dec 11 '22 08:12

cheapkid1


2 Answers

Seems an old question. But for anyone who stumble on this post, adding $ worked for me - & Environ$("username") &

like image 54
Suresh Avatar answered Feb 22 '23 21:02

Suresh


Or you could just put VBA. in front of it. This fixes it for me. i.e. Environ("Username") becomes VBA.Environ("Username").

like image 42
Tim Edwards Avatar answered Feb 22 '23 20:02

Tim Edwards