Help with Macro in Excel
One way:
Public Sub QuoteCommaMacro()
Const csQQ As String = """"
Const csDELIM As String = ","
' Dim all variables.
Dim DestFile As Variant
Dim FileNum As Long
Dim rRecord As Range
Dim rField As Range
Dim sOut As String
' Prompt for destination file
DestFile = Application.InputBox( _
Prompt:="Enter the destination filename" & _
vbNewLine & "(with complete path):", _
Title:="Quote-Comma Exporter", _
Default:=CurDir & Application.PathSeparator, _
Type:=2)
If DestFile = False Then Exit Sub 'user cancelled
' Get file handle number.
FileNum = FreeFile()
'Open Output File
On Error Resume Next
Open DestFile For Output As #FileNum
If Err < 0 Then
MsgBox "Cannot open filename " & DestFile
Exit Sub
End If
On Error GoTo 0
For Each rRecord In Selection.Rows
sOut = Empty
For Each rField In rRecord.Cells
With rField
If IsDate(.Text) Then
sOut = sOut & csDELIM & .Text
Else
sOut = sOut & csDELIM & csQQ & _
.Text & csQQ
End If
End With
Next rField
Print #FileNum, Mid(sOut, 2)
Next rRecord
Close #FileNum
End Sub
In article ,
surplusbc wrote:
Hi Everybody,
I have a bit of a problem with the below Macro.
This macro basically takes a selected area in a spreadsheet and creates
a text file. It adds quotes around each cell value and separates cells
within a row with commas. Each row has it's own line in the txt
format. So basically, click on any cell with a value, hit CTRL+Shft+*
to highlight the whole area, and then run the macro. You will be asked
to put a file name and path such as c:\mytextfiles\textfile.txt
As soon as you run the macro, a txt file will appear. It will show
something like:
"a","b","c","d"
"1","2","3","4"
Finally, my question. What I want to do is, if any value is in the
format of a date, I don't want quotes around it. Any ideas would be
appreciated.
|