• Excel Worksheets to Text (Excel 2000)

    Author
    Topic
    #411586

    I have several workbooks containing a various number of worksheets (50+) each. Is it possible to automate the process of exporting each worksheet of a workbook to a text file?

    Viewing 5 reply threads
    Author
    Replies
    • #893308

      This code will create a text file from each worksheet in the workbook and store the files in the directory C:Work with the same file name as the sheet name in the workbook.

      {pre]
      Public Sub CreateTxt()
      Dim oWS As Worksheet, oWB As Workbook
      Dim strPath As String, strFName As String
      strPath = “C:Work”
      For Each oWS In Worksheets
      oWS.Copy
      ActiveWorkbook.SaveAs Filename:=strPath & oWS.Name, FileFormat:=xlTextWindows
      Application.DisplayAlerts = False
      ActiveWorkbook.Close
      Application.DisplayAlerts = True
      Next oWS
      End Sub
      [/pre]

    • #893309

      This code will create a text file from each worksheet in the workbook and store the files in the directory C:Work with the same file name as the sheet name in the workbook.

      {pre]
      Public Sub CreateTxt()
      Dim oWS As Worksheet, oWB As Workbook
      Dim strPath As String, strFName As String
      strPath = “C:Work”
      For Each oWS In Worksheets
      oWS.Copy
      ActiveWorkbook.SaveAs Filename:=strPath & oWS.Name, FileFormat:=xlTextWindows
      Application.DisplayAlerts = False
      ActiveWorkbook.Close
      Application.DisplayAlerts = True
      Next oWS
      End Sub
      [/pre]

    • #893316

      Does something like this work? Change the path as appropriate and the fileformat if desired.

      Option Explicit
      Sub SaveEachAsText()
          Dim sOriName As String
          Dim wks As Worksheet
          Dim sPath As String
          Dim sFilename As String
          sOriName = ActiveWorkbook.FullName
          sPath = "C:"
          For Each wks In Worksheets
              sFilename = sPath & wks.Name & ".txt"
              Application.DisplayAlerts = False
              ActiveWorkbook.SaveAs _
                  FileName:=sFilename, _
                  FileFormat:=xlTextMSDOS
          Next
          ActiveWorkbook.SaveAs _
              FileName:=sOriName, FileFormat:=xlNormal
          Application.DisplayAlerts = True
      End Sub

      Note: this will overwrite without warning any files that already have that file name! Remove the “Displayalerts = false” line if you want a warning.

      Steve

    • #893317

      Does something like this work? Change the path as appropriate and the fileformat if desired.

      Option Explicit
      Sub SaveEachAsText()
          Dim sOriName As String
          Dim wks As Worksheet
          Dim sPath As String
          Dim sFilename As String
          sOriName = ActiveWorkbook.FullName
          sPath = "C:"
          For Each wks In Worksheets
              sFilename = sPath & wks.Name & ".txt"
              Application.DisplayAlerts = False
              ActiveWorkbook.SaveAs _
                  FileName:=sFilename, _
                  FileFormat:=xlTextMSDOS
          Next
          ActiveWorkbook.SaveAs _
              FileName:=sOriName, FileFormat:=xlNormal
          Application.DisplayAlerts = True
      End Sub

      Note: this will overwrite without warning any files that already have that file name! Remove the “Displayalerts = false” line if you want a warning.

      Steve

    • #893334

      Steve and Legare are too quick! One thought for both examples is that you might define the save location to be in the same folder as the current file, and to use the current file name as the first part of the file name, with:

      strPath = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) – 4)

      • #893336

        Thank you very much everyone … it works great. Now to maybe expand on this and get a little trickier. Is it possible to export a named range of a worksheet to a text file?

        • #893361

          Something like this?

          Option Explicit
          Sub RangeAsText()
              Dim wkbS As Workbook
              Dim wkbNew As Workbook
              Set wkbS = ActiveWorkbook
              Set wkbNew = Workbooks.Add
              wkbS.Activate
                  Range("ExportMe").Copy wkbNew.Sheets(1).Range("a1")
              Application.DisplayAlerts = False
              wkbNew.SaveAs _
                  FileName:="C:ExportName.txt", _
                  FileFormat:=xlTextMSDOS
              wkbNew.Close
              Application.DisplayAlerts = True
          End Sub

          Steve

        • #893362

          Something like this?

          Option Explicit
          Sub RangeAsText()
              Dim wkbS As Workbook
              Dim wkbNew As Workbook
              Set wkbS = ActiveWorkbook
              Set wkbNew = Workbooks.Add
              wkbS.Activate
                  Range("ExportMe").Copy wkbNew.Sheets(1).Range("a1")
              Application.DisplayAlerts = False
              wkbNew.SaveAs _
                  FileName:="C:ExportName.txt", _
                  FileFormat:=xlTextMSDOS
              wkbNew.Close
              Application.DisplayAlerts = True
          End Sub

          Steve

      • #893337

        Thank you very much everyone … it works great. Now to maybe expand on this and get a little trickier. Is it possible to export a named range of a worksheet to a text file?

    • #893335

      Steve and Legare are too quick! One thought for both examples is that you might define the save location to be in the same folder as the current file, and to use the current file name as the first part of the file name, with:

      strPath = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) – 4)

    Viewing 5 reply threads
    Reply To: Excel Worksheets to Text (Excel 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: