使用 excel vba 将数据写入文本文件
excel vba 在处理数据处理任务和清理时是一种流行的ag捕鱼王app官网的解决方案。此外,它可以自动执行需要花费大量时间才能完成的重复性任务。
vba 的关键功能之一是它能够生成和编辑文本文件,而无需手动打开、命名和保存更改。
本文将演示如何在 vba 的帮助下生成文本 (.txt)
文件。
在本文中,我们尝试使用 excel vba 生成文本文件。在深入研究代码之前,我们需要启用 excel vba 与处理文件和文件夹的 filesystemobject
进行交互。
启用 filesystemobject
的步骤:
现在一切就绪。
下面的代码块将演示使用 vba 创建文本文件。
savetexttofile
子程序需要两个参数,filename
和 filecontent
。filename
变量将是要生成的文本文件的所需文件名。filecontent
变量将是文本文件的实际内容。应该编辑 filepath
,使其适合输出文件的正确位置。
建议将 filesystemobject
声明为 fso
以启用 intellisense
的自动完成功能,这可以避免印刷错误,还有助于发现 filesystemobject
库中的其他方法。
在代码执行结束之前包含 fso = nothing
和 set filestream = nothing
很重要,因为如果这两个未关闭,运行时将发生错误。
option explicit
public sub savetexttofile(filename as string, filecontent as string)
dim filepath, fileattributes as string
filepath = "c:\users\temp\desktop\destination"
fileattributes = filepath & "" & filename & ".txt"
dim fso as filesystemobject
set fso = new filesystemobject
dim filestream as textstream
set filestream = fso.createtextfile(fileattributes)
filestream.writeline filecontent
filestream.close
if fso.fileexists(fileattributes) then
debug.print "file created successfully."
end if
set filestream = nothing
set fso = nothing
end sub
sub testsub()
call savetexttofile("sample1", "here is the content.")
end sub
上面的代码块实际上并不与 excel 交互,只生成一个文件。
下面的代码块将演示根据 excel 文件中的数据创建文本文件。有关示例值,请参阅 sheet1
。
工作表 1
:
| a | b | c |
1| file name | content | generatetextfile? |
2| sample1 | this is the content of sample1| yes |
2| sample2 | this is the content of sample2| no |
2| sample3 | this is the content of sample3| yes |
2| sample4 | this is the content of sample4| no |
2| sample5 | this is the content of sample5| yes |
2| sample6 | this is the content of sample6| no |
在下面的示例中,我们将重用 savetexttofile
子例程来创建文本文件。testsub
子程序将根据 sheet1
的数据循环调用 savetexttofile
。
option explicit
public sub savetexttofile(filename as string, filecontent as string)
dim filepath, fileattributes as string
filepath = "c:\users\temp\desktop\destination"
fileattributes = filepath & "" & filename & ".txt"
dim fso as filesystemobject
set fso = new filesystemobject
dim filestream as textstream
set filestream = fso.createtextfile(fileattributes)
filestream.writeline filecontent
filestream.close
if fso.fileexists(fileattributes) then
debug.print filename & " created successfully."
end if
set filestream = nothing
set fso = nothing
end sub
sub testsub()
dim wb as workbook
dim s1 as worksheet
set wb = thisworkbook
set s1 = wb.sheets("sheet1")
dim i as integer
i = 2
do until s1.cells(i, 1) = ""
if s1.cells(i, 3) = "yes" then
call savetexttofile(s1.cells(i, 1), s1.cells(i, 2))
end if
i = i 1
loop
end sub
testsub
输出:
sample1 created successfully.
sample3 created successfully.
sample5 created successfully.
sample1
、sample3
和 sample5
是创建的文本文件,因为它们在 generatetextfile
列中有 yes
。sample2
和 sample4
未创建,因为它们在 generatetextfile
列中为 no
。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2023/03/19 浏览次数:627 分类:vba
-
本教程演示了如何在 vba 中使用具有多个条件的自动过滤器。
发布时间:2023/03/19 浏览次数:383 分类:vba
-
本教程演示如何在 vba 中设置工作表。