Modifying macro - save workbook as text delimited file

dpnab

New Member
Joined
Apr 12, 2022
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I have the following code that saves the sheet as values into a new excel file (xlsx). I need to modify it so it saves it as a text file (tab delimited) instead. What do I need to change to make that happen? Thank you!

Sub Export_Sheets()
' Don't show confirmation window
Application.DisplayAlerts = False

Set wbk1 = ThisWorkbook
For Each sh In wbk1.Sheets
If sh.Name = "ShippingConfirmation" Then
Set wbk2 = Workbooks.Add
sh.Copy Before:=wbk2.Sheets(1)
wbk2.Sheets(sh.Name).UsedRange.Value = wbk2.Sheets(sh.Name).UsedRange.Value
wbk2.SaveAs Filename:=ThisWorkbook.Path & "/" & sh.Name & "txt"
wbk2.Close
End If
Next sh
' Allow confirmation windows to appear as normal
Application.DisplayAlerts = True

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Upvote 0
Do you know that you can Save As csv from the File menu? Tons of code examples for this though. Here's one


HTH.
Thanks for this. I will try a csv but the requirement is for it to be a tab delimited file, so a csv won't work?
 
Upvote 0
Have you tried...

VBA Code:
Sub Export_Sheets()
    ' Don't show confirmation window
    Application.DisplayAlerts = FALSE
    Set wbk1 = ThisWorkbook
    For Each sh In wbk1.Sheets
        If sh.Name = "ShippingConfirmation" Then
            Set wbk2 = Workbooks.Add
            sh.Copy Before:=wbk2.Sheets(1)
            wbk2.Sheets(sh.Name).UsedRange.Value = wbk2.Sheets(sh.Name).UsedRange.Value
            wbk2.SaveAs Filename:=ThisWorkbook.Path & "/" & sh.Name & "txt", FileFormat _
                        :=xlText, CreateBackup:=False
            wbk2.Close
        End If
    Next sh
    ' Allow confirmation windows to appear as normal
    Application.DisplayAlerts = TRUE
End Sub
 
Upvote 0
Solution
Have you tried...

VBA Code:
Sub Export_Sheets()
    ' Don't show confirmation window
    Application.DisplayAlerts = FALSE
    Set wbk1 = ThisWorkbook
    For Each sh In wbk1.Sheets
        If sh.Name = "ShippingConfirmation" Then
            Set wbk2 = Workbooks.Add
            sh.Copy Before:=wbk2.Sheets(1)
            wbk2.Sheets(sh.Name).UsedRange.Value = wbk2.Sheets(sh.Name).UsedRange.Value
            wbk2.SaveAs Filename:=ThisWorkbook.Path & "/" & sh.Name & "txt", FileFormat _
                        :=xlText, CreateBackup:=False
            wbk2.Close
        End If
    Next sh
    ' Allow confirmation windows to appear as normal
    Application.DisplayAlerts = TRUE
End Sub
That does it thanks! Any idea what I can add to only copy/paste the values of cells that have a value? I have some formulas that are IF statements that return a blank in certain scenarios. So if it's blank (the cell still has a formula) it shows up as blank lines in the txt file.
 
Upvote 0
For future consideration, there is also an option to save as tab delimited text file. Lots of other options in fact.
 
Upvote 0
That does it thanks! Any idea what I can add to only copy/paste the values of cells that have a value? I have some formulas that are IF statements that return a blank in certain scenarios. So if it's blank (the cell still has a formula) it shows up as blank lines in the txt file.
Not sure about this one.. Maybe someone else can have a go?😎
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top