Forum Discussion

Dirk Joye's avatar
Dirk Joye
Copper Contributor
Mar 14, 2018

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?

See annexed files: Excel01.xls = original Excel file; Excel01.txt = the Excel sheet saved as txt (tab delimited); Excel02.txt = the txt file I'd like to produce in one time by selecting the corresponding area in Excel.

Thanks for your help.

Dirk

1 Reply

  • Matt Mickle's avatar
    Matt Mickle
    Bronze 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

Resources