Macro help!

M

mx-3 for me

My macro is this..
Public Sub OutputQuotedCSV()
Const QSTR As String = """"
Dim myRecord As Range
Dim myField As Range
Dim nFileNum As Long
Dim sOut As String

nFileNum = FreeFile
Open "File1.txt" For Output As #nFileNum
For Each myRecord In Range("A1:A" & _
Range("A" & Rows.Count).End(xlUp).Row)
With myRecord
For Each myField In Range(.Cells(1), _
Cells(.Row, 256).End(xlToLeft))
sOut = sOut & "," & QSTR & _
Replace(myField.Text, QSTR, QSTR & QSTR)
QSTR
Next myField
Print #nFileNum, Mid(sOut, 2)
sOut = Empty
End With
Next myRecord
Close #nFileNum
End Sub

I want to tell it to NOT put quotes around numeric fields except thos
in column 1. Ideas anyone?
Thanks in advanced, ya'll are so smart on here
 
D

Dave Peterson

This seemed to work ok for me:

Option Explicit
Public Sub OutputQuotedCSV()
Const QSTR As String = """"
Dim myRecord As Range
Dim myField As Range
Dim nFileNum As Long
Dim sOut As String
Dim OneField As String

nFileNum = FreeFile
Open "File1.txt" For Output As #nFileNum
For Each myRecord In Range("A1:A" & _
Range("A" & Rows.Count).End(xlUp).Row)
With myRecord
For Each myField In Range(.Cells(1), Cells(.Row, 256).End(xlToLeft))
OneField = QSTR & Replace(myField.Text, QSTR, QSTR & QSTR) & QSTR
'if it's numeric and past column 1, then override that string.
If myField.Column > 1 Then
If Application.IsNumber(myField.Value) Then
OneField = myField.Text 'value????
End If
End If
sOut = sOut & "," & OneField
Next myField
Print #nFileNum, Mid(sOut, 2)
sOut = Empty
End With
Next myRecord
Close #nFileNum
End Sub
 
M

mx-3 for me

Wow thank you SO much! Thats a huge help for me. :)
Works perfectly just the way I need it to.
MAN! thanks so much
 

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