ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Put Excel data to text file???? (https://www.excelbanter.com/excel-programming/294961-put-excel-data-text-file.html)

bretweir

Put Excel data to text file????
 
This may be posted somewhere in this HUGE forum, but I couldn't fin
it.

I want to create a macro that selects a range of my choosing, copies i
(or just puts it) to a text file of my choosing. This will be a sor
of data archiving for an ever changing spreadsheet.

Thanks in advance for any help

--
Message posted from http://www.ExcelForum.com


Bernie Deitrick

Put Excel data to text file????
 
bretweir,

How about code to write a CSV? Select your range, and run the code below.

HTH,
Bernie
MS Excel MVP

Sub ExportToCSV()

Dim FName As String
Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer

'Change the folder path here - will create a dated-name csv file
FName = "C:\Excel\CSV" & Format(Now(), " yyyy mmm dd") & ".csv"

Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile

With Selection
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With

Open FName For Output Access Write As #FNum

For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If WholeLine = "" Then
WholeLine = Cells(RowNdx, ColNdx).Text
Else
WholeLine = WholeLine & "," & Cells(RowNdx, ColNdx).Text
End If
Next ColNdx
Print #FNum, WholeLine
Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

End Sub



"bretweir " wrote in message
...
This may be posted somewhere in this HUGE forum, but I couldn't find
it.

I want to create a macro that selects a range of my choosing, copies it
(or just puts it) to a text file of my choosing. This will be a sort
of data archiving for an ever changing spreadsheet.

Thanks in advance for any help.


---
Message posted from http://www.ExcelForum.com/





All times are GMT +1. The time now is 08:32 AM.

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