首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA:指定要保存的文件名和文件位置

问Excel VBA:指定要保存的文件名和文件位置
EN

Stack Overflow用户
提问于 2018-08-26 06:05:43
回答 1查看 629关注 0票数 0

我正在尝试使用Excel VBA自动保存一个文件。我需要使用以下命令保存文件:

(1)按日期动态命名: XYZ &今天的日期(例如"XYZ 20180825")

(2)静态位置:假设我需要将文件保存在"C:\Program Files“目录中。

我想知道如何在VBA中反映这一点。

EN

回答 1

Stack Overflow用户

发布于 2018-08-26 07:50:10

这是一个crashcourse,很抱歉出现了语言混合...

创建和写入文件

代码语言:javascript
复制
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

追加到现有文件

代码语言:javascript
复制
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

读取文件

代码语言:javascript
复制
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

删除文件

代码语言:javascript
复制
Sub LoescheDatei()
Dim strDateiname As String, strDateipfad As String

strDateiname = "test.txt"
strDateipfad = ThisWorkbook.Path & "\" & strDateiname

Kill(strDateipfad)
End Sub

更改名称,位置和包括日期时间,你会发现!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52021581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档