简体   繁体   English

将路径的特定部分作为变量的 Excel vba 函数

[英]Excel vba function with specific part of a path as variable

I have a function in excel that gives hour of last modification using path , only one part of the path changes exp:我在 excel 中有一个函数,它使用 path 给出最后修改的小时数,只有一部分路径更改 exp:

\\c:\\xcl\\report\\sudtrack\\20200324\\dossier22 \\c:\\xcl\\report\\sudtrack\\20200324\\dossier22

the part is 20200324零件是20200324

i want to do something like that : function('20200324') the code will place it in the path我想做这样的事情: function('20200324')代码会将它放在路径中

path="\\c:\xcl\report\sudtrack\" & 20200324 &"\dossier22"

my current code我当前的代码

Function End_hour(path As String)

    End_hour = Format(FileDateTime(path), "hh:mm:ss")

End Function

i want to do something like that : function('20200324') the code will place it in the path我想做这样的事情:function('20200324') 代码会将它放在路径中

No need to use use a function.无需使用使用功能。 You can directly do a Replace .您可以直接执行Replace Just set up a base string as shown below and do the replace.只需设置一个基本字符串,如下所示并进行替换。

Option Explicit

Sub Sample()
    Dim myPath As String

    myPath = "\\c:\xcl\report\sudtrack\HOUROFLASTMOD\dossier22"

    MsgBox Replace(myPath, "HOUROFLASTMOD", "20200324")
End Sub

Note : I have used HOUROFLASTMOD .注意:我已经使用了HOUROFLASTMOD You can change it to whatever string you want.您可以将其更改为您想要的任何字符串。

If you still want to use a function then try this如果你仍然想使用一个函数,那么试试这个

Option Explicit

Sub Sample()
    MsgBox ReturnNewPath("20200324")
End Sub

Function ReturnNewPath(TimeString As String)
    Dim myPath As String
    myPath = "\\c:\xcl\report\sudtrack\HOUROFLASTMOD\dossier22"

    ReturnNewPath = Replace(myPath, "HOUROFLASTMOD", TimeString)
End Function

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

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