文本文件需要保存为当前日期和时间格式

时间:2019-07-09 06:06:49

标签: excel vba

我已经在excel文件中提供了一些输入,一旦按下“提交”按钮,它应该在FileSavePath(e.x C:\ Temp)指定的目录中创建一个txt文件。该文件应命名为ddmmyyyyhhmmss.txt(例如09072019085633.txt)。

strFile_Path = "C:\temp\"
ActiveWorkbook.SaveAs ("C:\temp\ " & Format(Now(), "DDMMMYYYYhhmmss") & ".txt")

strFile_Path = "C:\temp\"
ActiveWorkbook.SaveAs ("C:\temp\ " & Format(Now(), "DDMMMYYYYhhmmss") & ".txt")

我希望一旦按下提交按钮,它就会在目标路径中以ddmmyyyyhhmmss.txt格式创建文本文件

1 个答案:

答案 0 :(得分:0)

Sub MakeTextFile()
 Dim fs, a As Object
 Dim dayPre, monthPre, hourPre, minutePre, secondPre As String

 If Day(Date) < 10 Then dayPre = "0"
 If Month(Date) < 10 Then monthPre = "0"
 If Hour(Time) < 10 Then hourPre = "0"
 If Minute(Time) < 10 Then minutePre = "0"
 If Second(Time) < 10 Then secondPre = "0"

 Set fs = CreateObject("Scripting.FileSystemObject")
 Set a = fs.CreateTextFile("C:\Temp\" & dayPre & Day(Date) & monthPre & Month(Date) & Year(Date) & hourPre & Hour(Time) & minutePre & Minute(Time) & secondPre & Second(Time) & ".txt", True)
 a.WriteLine ("This is a test.")
 a.Close

 Set a = Nothing
 Set fs = Nothing
End Sub