Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
majobojrod
 
Posts: n/a
Default convert xls to csv with quotes for alpha fields

What do you use to convert an XLS file to to a CSV text file, that contains
double quotes around every alphnumeric field, but NO double quotes around
numeric fields?

Thank you.

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default convert xls to csv with quotes for alpha fields

I'd use a macro.

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim myRng As Range
Dim myRow As Range
Dim myCell As Range
Dim fNum As Long
Dim myRec As String
Dim myVal As Variant

Set wks = Worksheets("sheet1")
With wks
Set myRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))

fNum = FreeFile
Open "C:\output.txt" For Output As fNum

For Each myRow In myRng.Rows
myRec = ""
For Each myCell In .Range(.Cells(myRow.Row, "A"), _
.Cells(myRow.Row, .Columns.Count).End(xlToLeft)).Cells
myVal = myCell.Value
If Application.IsNumber(myCell.Value) Then
'don't add the double quotes
Else
'double up any embeded double quotes
myVal = Application.Substitute(myVal, Chr(34), _
Chr(34) & Chr(34))
myVal = Chr(34) & myVal & Chr(34)
End If
myRec = myRec & "," & myVal
Next myCell
Print #fNum, Mid(myRec, 2)
Next myRow

Close fNum

End With
End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

majobojrod wrote:

What do you use to convert an XLS file to to a CSV text file, that contains
double quotes around every alphnumeric field, but NO double quotes around
numeric fields?

Thank you.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
majobojrod
 
Posts: n/a
Default convert xls to csv with quotes for alpha fields

Dave,

Perfect! Thank you so much.

John Rodrigues

"Dave Peterson" wrote:

I'd use a macro.

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim myRng As Range
Dim myRow As Range
Dim myCell As Range
Dim fNum As Long
Dim myRec As String
Dim myVal As Variant

Set wks = Worksheets("sheet1")
With wks
Set myRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))

fNum = FreeFile
Open "C:\output.txt" For Output As fNum

For Each myRow In myRng.Rows
myRec = ""
For Each myCell In .Range(.Cells(myRow.Row, "A"), _
.Cells(myRow.Row, .Columns.Count).End(xlToLeft)).Cells
myVal = myCell.Value
If Application.IsNumber(myCell.Value) Then
'don't add the double quotes
Else
'double up any embeded double quotes
myVal = Application.Substitute(myVal, Chr(34), _
Chr(34) & Chr(34))
myVal = Chr(34) & myVal & Chr(34)
End If
myRec = myRec & "," & myVal
Next myCell
Print #fNum, Mid(myRec, 2)
Next myRow

Close fNum

End With
End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

majobojrod wrote:

What do you use to convert an XLS file to to a CSV text file, that contains
double quotes around every alphnumeric field, but NO double quotes around
numeric fields?

Thank you.


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert spreadsheet to csv with double quotes Bren Lane Excel Discussion (Misc queries) 3 September 19th 06 01:02 AM
Quotes around fields in CSV SWMagic Excel Discussion (Misc queries) 1 January 19th 06 09:32 PM
CONVERT Function Disappered in Excel Gord Dibben Excel Discussion (Misc queries) 3 April 13th 05 07:59 PM
i have fields that are formatted for text that randomly convert t. dave glynn Excel Discussion (Misc queries) 2 February 22nd 05 09:03 PM
Convert Numeric into Text Monty Excel Worksheet Functions 0 December 18th 04 09:25 PM


All times are GMT +1. The time now is 05:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"