Computing.Net > Forums > Office Software > Cell Range To Text File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Cell Range To Text File

Reply to Message Icon

Name: Dominican1979
Date: September 18, 2008 at 16:02:30 Pacific
OS: XP Pro
CPU/Ram: Centrino Duo/1GB
Product: Toshiba Tecra
Comment:

Hello Everyone,

I need to export a cell range that a user specifies to a text file delimited by a coma. They may select for example two columns, and x number of rows and run a macro that will export only the range they selected to a text file in this format below:

11966031,Test 1
56565656,Test 2
54545454,Test 3
44545454,Test 4

This output textfile should be overwritten with the new range every time they run the macro.
Thanks a lot in advance for your help, I greatly appreciate it

MS



Sponsored Link
Ads by Google

Response Number 1
Name: jon_k
Date: September 22, 2008 at 05:29:54 Pacific
Reply:

A solution. Had to do a bit of recording to see how it all hung together, so no doubt this code could be improved. Also be aware you may run into problems with commas in text fields, haven't really tested for that.

START OF CODE

Sub moverng()
Application.DisplayAlerts = False
Set myrng = ActiveWindow.RangeSelection

Set newbook = Workbooks.Add
With newbook
.Title = "export"
.SaveAs Filename:="c:\export.csv", FileFormat:=xlCSV
End With

myrng.Copy
Windows("export.csv").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks("export.csv").Save
Workbooks("export.csv").Close
Application.DisplayAlerts = True
MsgBox ("selection successfully copied to c:\export.csv")

End Sub

END OF CODE


0

Response Number 2
Name: Dominican1979
Date: September 22, 2008 at 08:04:11 Pacific
Reply:

OMG Jon, thank you sooooooooooo much, you have no idea how much you have helped me, I've been dealing with this part of a little project since last week and have been looking everywhere for an answer. I had managed to export a file doing this macro below, but it wasn't giving me quite what I was looking for. Thanks a million!!!!!!!!!!!

Sub WriteIt()
WriteFile Range("B2", Cells(Rows.Count, 1).End(xlUp).Address), _
ThisWorkbook.Path & "\MyFile.txt"
End Sub

Sub WriteFile(fromRange As Range, toFile As String)
Dim iHandle As Integer, cell As Range
iHandle = FreeFile
Open toFile For Output Access Write As #iHandle
For Each cell In fromRange
Print #iHandle, cell.Offset(1, 1).Value
Next cell
Close #iHandle
End Sub

MS


0

Response Number 3
Name: jon_k
Date: September 22, 2008 at 09:01:17 Pacific
Reply:

Glad I could help!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Cell Range To Text File

How can I Convert my Image Files to Text File www.computing.net/answers/office/how-can-i-convert-my-image-files-to-text-file/9574.html

Text file to Access help www.computing.net/answers/office/text-file-to-access-help/499.html

Importing text file to EXCEL www.computing.net/answers/office/importing-text-file-to-excel/4176.html