1

I'm currently trying to get an excel file to save into YYYYMMDD_fixed name piece_INITIALS OF LAST PERSON TO EDIT.

I'm using Environ function to call the User's and PC's name in a cell that i've found can be used to add to the name.

The issues i'm trying to fix are:

  1. how can i define the save path to work on any PC regardless of user name, as current path has Users/my name/ , and up to 4 people with different PCs and names will edit this file. it should just save on Desktop on any of the 4 PCs

  2. how can i modify the

strFile = "C:\Users\me\Desktop\" & Format(dtDate, "ddmmyyyy") & ".xlsx"

part so that it displays YYYYMMDD_name (i get this part ok) _ABC where ABC value is in cell A1 generated by the below attr function?

the function used is

Function attr(choice) As String
   Select Case (choice)
      Case "computer": attr = Environ("Computername")
      Case "user": attr = Environ("UserName")
   End Select
End Function

and the one i use to save (albeit a different format on a different file) is

Dim dtDate As Date
    dtDate = Date

    Dim strFile As String
    strFile = "C:\Users\me\Desktop\" & Format(dtDate, "ddmmyyyy") & ".xlsx"

    ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
    :=51, CreateBackup:=False

Any help would be greatly appreciated! Programming is not my main job, just trying to automate bits where possible, so go easy on me please :)

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
ciuncky
  • 15
  • 4

1 Answers1

0

Maybe something like that will help:

Dim strFile As String, strUserName As String
Dim dtDate As Date

dtDate = Now
strUserName = attr("user")

strFile = "C:\Users\" & strUserName & "\Desktop\" & Format(dtDate, "ddmmyyyy") & "_" & Sheets("Sheet1").Range("A1").Value & ".xlsx"
MsgBox strFile

Note that I assigned the value of an active username to strUserName and I'm using it with your strFile. I also added Sheets("Sheet1").Range("A1").Value to the code (change sheet name accordingly). The final result will look like that:

C:\Users\username\Desktop\12082019_username.xlsx

Justyna MK
  • 3,523
  • 3
  • 11
  • 25
  • 1
    Thank you! That works brilliantly, my monday morning caffeine-UNinfused brain ignored the MsgBox strFile part and couldn't understand why the popup! Extra candies for adding the sheet part, i actually need to pick a particular one out of 8 so thanks for thinking in advance! – ciuncky Aug 12 '19 at 11:49
  • No problem at all (I know a thing or two about caffeine :)). Feel free to upvote/accept the answer if it was useful - see: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Justyna MK Aug 12 '19 at 12:41
  • already voted, but seems it won't count due to my low/novice rating. didn't know about the checkmark thingy! :) – ciuncky Aug 12 '19 at 13:09
  • Thanks! The more points you get, the more options/privileges you will have. Have fun & also try to help other people with their questions. Btw welcome to the forum :) – Justyna MK Aug 12 '19 at 13:11