简体   繁体   English

VBA-将Excel工作表导出到.txt文件

[英]VBA - Export excel sheet to a .txt file

I am trying to export a particular column from a excel sheet into .txt file format. 我正在尝试将Excel表格中的特定列导出为.txt文件格式。 I would like to allow the user to provide name and location of the file to be saved. 我想允许用户提供要保存的文件的名称和位置。

I was able to save it in .xlsx format. 我能够将其保存为.xlsx格式。 Anyone know how to export it into .dat format ? 有人知道如何将其导出为.dat格式吗?

This is my code for .xlsx export, 这是我的.xlsx导出代码,

Workbooks.Add
ActiveSheet.Paste
Cells.Select
Selection.Columns.AutoFit
Application.CutCopyMode = False

sFileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="Excel Files (*.xlsx), *.xls")

        If sFileSaveName <> False Then
        ActiveWorkbook.SaveCopyAs sFileSaveName
        MsgBox "File Successfully Saved!"
        ActiveWorkbook.Close
        End if

I have tried something like this, 我已经尝试过这样的事情,

myFile = Application.GetSaveAsFilename(filefilter:="Text Files (*.txt),*txt")

But my output got messed up completely and looks like this - 但是我的输出完全混乱了,看起来像这样- 文字输出

Thanks. 谢谢。

您必须包括一种保存为文本文件的文件格式: FileFormat:=xlText

ActiveWorkbook.SaveAs Filename:=YourTextFile.txt, FileFormat:=xlText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM