ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to Create text file from range of cells? (https://www.excelbanter.com/excel-programming/305339-macro-create-text-file-range-cells.html)

Joan Roco[_2_]

Macro to Create text file from range of cells?
 
Hi folks,
I hope you can help me on this.
It will greatly simplify my life:

I need a simple macro to create a text file from a range
of cells.

Thanks,
-joan

P.S. to email me directly, remove nospam from the address.

Jim Rech

Macro to Create text file from range of cells?
 
''Outputs the selection if more than one cell is selected, else entire sheet
Sub CreateCSVFile()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
If FName < False Then
ListSep = Application.International(xlListSeparator)
'ListSep = "," 'use this to force commas as separator regardless of
regional settings
If Selection.Cells.Count 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & CurrCell.Value & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End If
End Sub


--
Jim Rech
Excel MVP
"Joan Roco" wrote in message
...
| Hi folks,
| I hope you can help me on this.
| It will greatly simplify my life:
|
| I need a simple macro to create a text file from a range
| of cells.
|
| Thanks,
| -joan
|
| P.S. to email me directly, remove nospam from the address.




All times are GMT +1. The time now is 12:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com