简体   繁体   中英

VBA - Export excel sheet to a .txt file

I am trying to export a particular column from a excel sheet into .txt file format. 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. Anyone know how to export it into .dat format ?

This is my code for .xlsx export,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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