将今天的日期添加到文件名

时间:2019-08-08 20:46:36

标签: excel vba

我需要将今天的日期添加到文件名中。

我有一部分代码是从另一个文件复制的,但没有该功能。

我需要更改为今天日期的地方是“ /CARYYMMDD2428395101.BCA”。

Sub Export_Selection_As_Fixed_Length_File()
 ' Dimension all  variables.
Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String
Dim FileNum, ColumnCount, RowCount, FieldWidth As Integer
Dim sht As Worksheet

'Below are options incase you want to change the folder where VBA stores the .txt file
'We use ActiveWorkbook.Path in this example
'ActiveWorkbook.Path 'the activeworkbook
'ThisWorkbook.Path  'the workbook with the code
'CurDir  'the current directory (when you hit File|open)


'If a cell is blank, what character should be used instead
Filler_Char_To_Replace_Blanks = " "

    'Check if the user has made any selection at all
    If Selection.Cells.Count < 2 Then
        MsgBox "No row has been selected"
        Selection.Activate
        End
    End If

'This is the destination file name.
DestinationFile = ActiveWorkbook.Path & "/CARYYMMDD24284444101.BCA"
'Obtain next free file handle number.
FileNum = FreeFile()

我希望得到文件名为CAR19080824284444101.BCA

3 个答案:

答案 0 :(得分:1)

首先,我想指出您对变量的限定不正确。第choices: "sh 'ls /build/libs/*.jar"行仅将sh 'ls /build/libs/*.jar"声明为Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String,其余均为Filler_Char_To_Replace_Blanks类型。

String是您想要的,您可以更改格式,但是,我在下面演示了另一种命名方法,但是如果您喜欢我在这里的内容,请对其进行修改。

Variant

答案 1 :(得分:1)

这是一些伪代码,它将'ThisWorkbook'保存到指定的路径(例如C:\ Test目录)中,并在文件名的末尾添加日期

ThisWorkbook.SaveCopyAs <declare_path_variable> & **Format(Date, "dd-mm-yyyy")** & ThisWorkbook.Name

答案 2 :(得分:0)

您可以这样做:

'Name of the Excel file with a date/time stamp
XLSFileName = DefPath & "MasterCSV " & _
              Format(Now, "dd-mmm-yyyy h-mm-ss") & FileExtStr