create TEXT files for EACH row in EACH xls Spreadsheet

S

schiffkey

Hi,
I was able to modify Microsoft's example of how to
export rows FROM a spreadsheet TO a TEXT file.

In the modification below,
a text file is created for each row of data selected.
(and within the text file, a header row is created as
well).

Problem:
If you have more than ONE excel spreadsheet, in fact, 30
incoming spreadsheets a week - is there a program that
does not rely on macros? ie can be run from windows or a
DOS prompt?
reply to (e-mail address removed)


Sub QuoteCommaExport()
' Dimension all variables.
Dim DestFile As String
Dim DestDir As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer

' Prompt user for destination file name.
' DestFile = InputBox("Enter the destination
filename" _
' & Chr(10) & "(with complete path):", "Quote-
Comma Exporter")
DestDir = "c:\my documents"

DestDir = InputBox("Enter the destination
directory AND File Name" _
& Chr(10) & "(ie: c:\my
documents\FLmrt)", "Quote-Comma Exporter")
FileNum = 0
' Loop for each row in selection.
For RowCount = 1 To Selection.Rows.Count
' Close destination file.
Close #FileNum
' Obtain next free file handle number.
FileNum = FileNum + 1
' Turn error checking off.
On Error Resume Next
' Attempt to open destination file for output.
DestFile = DestDir & FileNum & ".txt"
Open DestFile For Output As #FileNum
' If an error occurs report it and end.
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
' Turn error checking on.
On Error GoTo 0

' headings ?
' Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.Count
' Write current cell's text to file with
quotation marks.
Print #FileNum, """" & Worksheets
("Sheet1").Cells(1, _
ColumnCount).Text & """";
' Check if cell is in last column.
If ColumnCount = Selection.Columns.Count
Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ",";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount

' Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.Count
' Write current cell's text to file with
quotation marks.
Print #FileNum, """" & Selection.Cells
(RowCount, _
ColumnCount).Text & """";
' Check if cell is in last column.
If ColumnCount = Selection.Columns.Count
Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ",";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount

' Start next iteration of RowCount loop.
Next RowCount
' Close destination file.
Close #FileNum
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top