在一个模块中使用不同格式的Date变量

问题描述 投票:0回答:1

我试图设置今天的日期,并以不同的格式使用它来完成各种任务,但我不知道如何去做。我知道Date是一个全局变量,但它在我们的系统上采用2017-12-19格式,对于某些任务我需要它是mm-dd-yyyy或只是mm-dd但我不知道如何在不提示用户手动的情况下设置它以上述格式输入日期。我也可以像字符串一样使用Date变量,还是需要转换它?

Sub BasedOnDate()
Dim filename As String, Source As String, Destination As String
Dim myValx As String
Dim myVal1 As Date
Dim myValn As Date
Dim myFile As String
Dim myPath As String

'trying to declare today's date, but just the month and day as "mm-yy"
myVal1 = Date("mm-yy")
myValn = Replace(myVal1, "-", "\")

'trying to declare the date here in the desired format
myValx = Date("mm-dd-yyyy")

'File paths
Source = "\\Rdcicgtcuwd01p\app_log\36196_WMS\WMS_36196_PROD_" & myValx & ".csv"
Destination = "\\olscmesf003\gcm_emea\TCU_REPORTS\APPS\Reports\Regional\Workflow Management System\2017\" & myValn


FileCopy Source, Destination


End Sub
excel vba
1个回答
1
投票

你可以使用Format() function

myValx = Format(Date, "mm-dd-yyyy")

提示:请记住,Format()返回字符串,这里工作正常,但在其他情况下可能是一个问题。

© www.soinside.com 2019 - 2024. All rights reserved.