zfitzjarrell
Board Regular
- Joined
- Mar 9, 2005
- Messages
- 114
I found some really good code on this board, but it doesn't do exactly what I'm looking for and my attempts to modify the code haven't worked.
I'm trying to created a comma delimited text file. Here is the code I'm working with:
I tried to change the Delimiter part by putting a comma inside the quotes, but it still creates a tab delimited txt file.
Any ideas?
thanks
I'm trying to created a comma delimited text file. Here is the code I'm working with:
Code:
Sub ExportToText()
Dim delimiter As String
Dim quotes As Integer
Dim Returned As String
delimiter = " "
' Specify if you want quotes to surround the cell information
' (0 = no 1 = yes)
quotes = 0
' Call the WriteFile function passing the delimiter and quotes options.
Returned = WriteFile(delimiter, quotes)
' Print a message box indicating if the process was completed.
Select Case Returned
Case "Canceled"
MsgBox "The export operation was canceled."
Case "Exported"
MsgBox "The information was exported."
End Select
End Sub
_______________________________________________________________________
Function WriteFile(delimiter As String, quotes As Integer) As String
' Dimension variables to be used in this function.
Dim CurFile As String
Dim SaveFileName
Dim CellText As String
Dim RowNum As Integer
Dim ColNum As Integer
Dim FNum As Integer
Dim TotalRows As Double
Dim TotalCols As Double
Dim HalfWidth As Integer
' Show Save As dialog box with the .TXT file name as the default.
' Test to see what kind of system this macro is being run on.
If Left(Application.OperatingSystem, 3) = "Win" Then
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"Text Delimited (*.txt), *.txt", , "Export to a Text File")
Else
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"TEXT", , "Text Delimited Exporter")
End If
' Check to see if Cancel was clicked.
If SaveFileName = False Then
WriteFile = "Canceled"
Exit Function
End If
' Obtain the next free file number.
FNum = FreeFile()
' Open the selected file name for data output.
Open SaveFileName For Output As #FNum
' Store the total number of rows and columns to variables.
TotalRows = Selection.Rows.Count
TotalCols = Selection.Columns.Count
' Loop through every cell, from left to right and top to bottom.
For RowNum = 1 To TotalRows
For ColNum = 1 To TotalCols
With Selection.Cells(RowNum, ColNum)
Dim ColWidth As Integer
ColWidth = Application.RoundUp(.ColumnWidth, 0)
' Store the current cells contents to a variable.
If ColWidth < Len(.Text) Then
CellText = Left(.Text, ColWidth)
Else
Select Case .HorizontalAlignment
Case xlRight
CellText = Space(ColWidth - Len(.Text)) & .Text
Case xlCenter
HalfWidth = (ColWidth - Len(.Text)) / 2
CellText = Space(HalfWidth) & .Text & _
Space(ColWidth - Len(.Text) - HalfWidth)
Case Else
CellText = .Text & Space(ColWidth - Len(.Text))
End Select
End If
End With
' Write the contents to the file.
' With or without quotation marks around the cell information.
Select Case quotes
Case vbYes
CellText = Chr(34) & CellText & Chr(34) & delimiter
Case vbNo
End Select
Print #FNum, CellText;
' Update the status bar with the progress.
Application.StatusBar = Format((((RowNum - 1) * TotalCols) _
+ ColNum) / (TotalRows * TotalCols), "0%") & " Completed."
' Loop to the next column.
Next ColNum
' Add a linefeed character at the end of each row.
If RowNum <> TotalRows Then Print #FNum, ""
' Loop to the next row.
Next RowNum
' Close the .prn file.
Close #FNum
' Reset the status bar.
Application.StatusBar = False
WriteFile = "Exported"
End Function
I tried to change the Delimiter part by putting a comma inside the quotes, but it still creates a tab delimited txt file.
Any ideas?
thanks