如何在VBA中另存为.txt [英] How to save as .txt in vba

查看:279
本文介绍了如何在VBA中另存为.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让我的宏保存一个我创建为.txt文件的新工作表.这是我到目前为止的代码.

I am looking to have my Macro save a new sheet that i created as a .txt file. this is the code i have so far.

Sub Move()  
'  
' Move Macro  
'  
' Keyboard Shortcut: Ctrl+m  
'  
Sheets("Sheet1").Select  
Range("A1").Select  
Range(Selection, Selection.End(xlToRight)).Select  
Range(Selection, Selection.End(xlDown)).Select  
Selection.Copy  
Workbooks.Add  
ActiveSheet.Paste  

ActiveWorkbook.SaveAs Filename:="e:" & _
"HDR" + Format(Now(), "YYYYMMDDhhmmss") & ".txt"

End Sub

其中包括我的宏.我无法将其另存为.txt文件.
我目前在.txt文件上出现了很多废话,这是一个示例,
"PK!!}ñU{Š[Content_Types] .xml¢(ÌTÝN¾¾ññÞš€‰1†Á…âˆPˆ3¶ÐµMOÁñöž•Ÿ¨¨."
任何帮助将是巨大的.

That includes my macro. I am having trouble with the last part where it saves as a .txt file.
I am currently getting a bunch of crap on my .txt file, here is an example,
"PK ! !}ñU{ Š [Content_Types].xml ¢(  ÌTÝNÂ0¾7ñ–Þš­€‰1†Á…⥒ˆPÚ3¶ÐµMOÁñöž•Ÿ¨".
Any help would be great.

推荐答案

手动更改文件名的扩展名实际上不会更改文件类型. SaveAs方法采用文件类型参数.您想要的代码是

Manually changing the extension of the file name does not actually change the file type. The SaveAs method takes a file type argument. The code you want is

ActiveWorkbook.SaveAs Filename:="e:" & "HDR" + Format(Now(), "YYYYMMDDhhmmss") _
& ".txt", FileFormat:= xlTextWindows

通过Excel帮助中的XlFileFormat搜索将(几乎)为您提供所有可能的文件格式的完整列表,包括6种文本格式和4种CSV格式.

Doing a search from within Excel help for XlFileFormat will get you (almost) the full list of possible file formats, including 6 text formats, and 4 CSV formats.

这篇关于如何在VBA中另存为.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆