Forum Discussion
Dirk Joye
Mar 14, 2018Copper Contributor
Print partial Excel sheet to text file
From one single sheet in an Excel file I want to export/print a part to a txt file. Can I do that by selecting the wanted area and print only this to a txt file or how can I do this in a simple way? ...
Matt Mickle
Mar 16, 2018Bronze Contributor
You can use this macro to export a range selection to a .txt file.
Source: https://www.extendoffice.com/documents/excel/612-excel-export-data-to-text.html#export-selection-to-text-vba
Sub ExportRangetoFile()
'Update 20130913
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub