简体   繁体   English

在excel中编辑自动命名宏(基于PC名称和日期,具有可变的保存路径)

[英]Editing an automated naming macro in excel (based on PC name and date, with variable save path)

I'm currently trying to get an excel file to save into YYYYMMDD_fixed name piece_INITIALS OF LAST PERSON TO EDIT. 我目前正在尝试获取一个Excel文件,以保存到YYYYMMDD_fixed名称piece_INITIALS OF LAST PERSON 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. 我正在使用Environ函数在我发现可以用于添加名称的单元格中调用用户名和PC的名称。

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. 我如何定义保存路径以在任何PC上工作,无论用户名如何,因为当前路径具有Users / my name /,并且最多4个人使用不同的PC和名称将编辑此文件。 it should just save on Desktop on any of the 4 PCs 它应该只保存在4台PC的任何一台的Desktop上

  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? 部分,以便显示YYYYMMDD_name(我可以正常使用此部分)_ABC其中ABC值在下面的attr函数生成的单元格A1中?

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 :) 编程不是我的主要工作,只是尝试在可能的情况下自动执行位,所以请对我轻松一点:)

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 . 请注意,我strUserName活动用户名的值分配给strUserName并将其与您的strFile I also added Sheets("Sheet1").Range("A1").Value to the code (change sheet name accordingly). 我还向代码添加了Sheets("Sheet1").Range("A1").Value (相应地更改工作表名称)。 The final result will look like that: 最终结果将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM