我正在尝试使用Excel VBA自动保存一个文件。我需要使用以下命令保存文件:
(1)按日期动态命名: XYZ &今天的日期(例如"XYZ 20180825")
(2)静态位置:假设我需要将文件保存在"C:\Program Files“目录中。
我想知道如何在VBA中反映这一点。
发布于 2018-08-26 07:50:10
这是一个crashcourse,很抱歉出现了语言混合...
创建和写入文件
Sub SchreibeDatei()
Dim strDateiname As String, strDateipfad As String
strDateiname = Date & Time & "test.txt"
strDateipfad = ThisWorkbook.Path & "\" & strDateiname
' Datei erzeugen und/oder schreiben
Open strDateipfad For Output As #1
Print #1, ""
Print #1, ""
Close #1
End Sub追加到现有文件
Sub ErweitereDatei()
Dim strDateiname As String, strDateipfad As String
strDateiname = "test.txt"
strDateipfad = ThisWorkbook.Path & "\" & strDateiname
Open strDateipfad For Append As #1
Print #1, ""
Close #1
End Sub读取文件
Sub LeseDatei()
Dim strDateiname As String, strDateipfad As String
Dim strZeile As String, strInhalt As String
strDateiname = "test.txt"
strDateipfad = ThisWorkbook.Path & "\" & strDateiname
' Datei zeilenweise lesen
Open strDateipfad For Input As #1
While Not EOF(1) ' EOF = EndOfFile
Line Input #1, strZeile
strInhalt = strInhalt + strZeile & Chr(10)
Wend
Close #1
MsgBox strInhalt
End Sub删除文件
Sub LoescheDatei()
Dim strDateiname As String, strDateipfad As String
strDateiname = "test.txt"
strDateipfad = ThisWorkbook.Path & "\" & strDateiname
Kill(strDateipfad)
End Sub更改名称,位置和包括日期时间,你会发现!
https://stackoverflow.com/questions/52021581
复制相似问题