Saving a Dynamic Range as a .txt file

RichardElk

New Member
Joined
Feb 7, 2014
Messages
8
Hello All,

I have done some searching/experimenting and have been unable to find an adequate solution for my problem.

What I would like to do is use a macro to copy a range of cells (N7:N####, where the value of #### is defined in cell O7) into a text file that uses the current date as the default file name and saves the .txt file into a directory of the user's choosing. The text within the range are formatted exactly as required in the text file and can not have additional characters added.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Problem with text files is they don't support a lot of Formating

But anyway

Code:
Sub pasteSelectionTotxt()
Dim Printvar As Variant
Dim Mycell As Range

ActiveSheet.Range("G7:G" & Range("O7").Value).Select

Open "C:\TEMP\" & Format(Now(), "DD-MMM-YYYY") & ".txt" For Output As #1

For Each Mycell In Selection
   Print #1, Mycell.Value
Next Mycell
Close #1
End Sub

I obviously used G instead of N

This writes out a Column of cells as a text file
 
Last edited:
Upvote 0
CharlesCC, You. Are. The. Man.

It worked like a charm. Tweaked it a bit with some user prompts and she was ready to roll.

I cant thank you enough.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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