Create a Macro and Button to export a single column as .txt file

abcdefghi

New Member
Joined
Aug 27, 2021
Messages
2
Office Version
  1. 365
  2. 2016
I want to export all contents in column E as a .txt file and have the filename from a value of a single cell example (H1).

I'm not familiar macro so any code will really help. The value in column E is actually just a concatenation of the values in A to D with a "|" in the middle so a code to export column E or just to combine columns A to D with a | separator should work
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this..
VBA Code:
Sub test()
    Dim fName$, i&
    With Sheets(1)
        fName = ThisWorkbook.Path & "\" & .Range("H1").Value
        Open fName For Output As #1

        For i = 1 To .Cells(Rows.Count, 1).End(3).Row
            t$ = .Cells(i, 1).Value
            For ii = 2 To 5
                t$ = t$ & "|" & .Cells(i, ii).Value
            Next ii
            Print #1, t$
        Next i
        Close #1
    End With
End Sub
 
Upvote 0
Which should I edit here? I replaced "\" with the path where I wanted to save the output.

After trying to run the macro, it said "Bad File Name or Number"

I just want to export Column E into a .txt file and have the file name of that with whatever value is in H1
 
Upvote 0
This macro does exactly what you want. It creates a text file in the folder where the excel file is located, according to the name in H1.

What exactly does it say in H1?

By the way
Rich (BB code):
For ii = 2 To 4
fix it.
 
Upvote 0

Forum statistics

Threads
1,216,190
Messages
6,129,422
Members
449,509
Latest member
ajbooisen

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