Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 4This 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

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.RangeSelectionSet 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

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 SubSub 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 SubMS

![]() |
![]() |
![]() |

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