wrting spreadsheet to csv file

T

tomek

In spreadsheet I have Monitor 15" in A1.
I wrtie this spreadsheet as txt file where values
are seperated with tabular sign and after that when I edit
this file I have "Monitor 15""". Why Excell adds this 3 """" ?
Is there anything I can do to force excel not to add unexpected " ?
 
B

Bernard Liengme

It is the inch symbol (") that is doing it. Why not use: Monitor 15 in
best wishes
 
R

Rick Rothstein \(MVP - VB\)

In spreadsheet I have Monitor 15" in A1.
I wrtie this spreadsheet as txt file where values
are seperated with tabular sign and after that when I edit
this file I have "Monitor 15""". Why Excell adds this 3 """" ?
Is there anything I can do to force excel not to add unexpected " ?

If your data is not too "complex" (the following is not a fully robust CSV
file creator, but it should correctly handle the vast majority of data types
one would normally have), you can use the code following my signature to
write out your data the way you indicated you want. Copy/Paste all of the
code below my signature into an Module (Insert/Module from VB editor's menu
bar) for maximum flexibility (the code can be called from any sheet in your
workbook) or into a specific sheet's code window if you know it will only be
run form that one worksheet. Your code simply needs to call PrintCVSfile
with the sheet you want to be outputted as the active sheet.

Rick

Sub PrintCVSfile()
Dim FF As Long
Dim X As Long, Y As Long
Dim StartRow As Long, EndRow As Long
Dim StartColumn As Long, EndColumn As Long
Dim LineOfText As String
Dim FileName As Variant
FileName = Application.GetSaveAsFilename(fileFilter:= _
"CSV (Comma Separated) (*.csv), *.csv")
If FileName = False Then Exit Sub
ActualUsedRange StartRow, EndRow, StartColumn, EndColumn
FF = FreeFile
Open FileName For Output As #FF
For X = StartRow To EndRow
LineOfText = ""
For Y = StartColumn To EndColumn
If InStr(ActiveSheet.Cells(X, Y).Value, ",") Then
LineOfText = LineOfText & """" & ActiveSheet.Cells(X, Y) & """"
Else
LineOfText = LineOfText & ActiveSheet.Cells(X, Y)
End If
If Y < EndColumn Then LineOfText = LineOfText & ","
Next
Print #FF, LineOfText
Next
Close #FF
End Sub

Sub ActualUsedRange(StartRow, EndRow, StartColumn, EndColumn)
Dim X As Long
If VarType(StartRow) > 6 Or VarType(EndRow) > 6 Or _
VarType(StartColumn) > 6 Or VarType(EndColumn) > 6 Then
MsgBox "Numeric data types only!", vbCritical, "Data Type Error"
Exit Sub
End If
With ActiveSheet.UsedRange
StartRow = .Row + .Rows.Count - 1
StartColumn = .Column + .Columns.Count - 1
For X = .Row To .Row + .Rows.Count - 1
With ActiveSheet.Cells(X, Columns.Count)
If EndColumn < .End(xlToLeft).Column Then
EndColumn = .End(xlToLeft).Column
End If
End With
With ActiveSheet.Cells(X, 1)
If ActiveSheet.Cells(X, 1).Value <> "" Then
StartColumn = 1
ElseIf StartColumn > .End(xlToRight).Column Then
StartColumn = .End(xlToRight).Column
End If
End With
Next
For X = .Column To .Column + .Columns.Count - 1
With ActiveSheet.Cells(Rows.Count, X)
If EndRow < .End(xlUp).Row Then
EndRow = .End(xlUp).Row
End If
End With
With ActiveSheet.Cells(1, X)
If ActiveSheet.Cells(1, X).Value <> "" Then
StartRow = 1
ElseIf StartRow > .End(xlDown).Row Then
StartRow = .End(xlDown).Row
End If
End With
Next
End With
End Sub
 
A

Alex Turner

In spreadsheet I have Monitor 15" in A1. I wrtie this spreadsheet as txt
file where values are seperated with tabular sign and after that when I
edit this file I have "Monitor 15""". Why Excell adds this 3 """" ? Is
there anything I can do to force excel not to add unexpected " ?

The way I would handle this is to write a script which opens the tab
separated value file, replaces every instance of "" with " and then saves
it again. There is an example of doing this sort of thing in the code
for my book. Which you can find if you follow the link below :)
 
E

Earl Kiosterud

Since the text qualifier is normally a quote mark ("), it's proper and necessary to put the
whole thing in quotes, then put in a pair of quotes ("double"-quotes?) for the one quote
mark. Any implementation where you actually wind up with only one quote mark will likely
cause trouble in any program reading and parsing (separating the fields) the file. For some
background, take a look at "Delimited Text File" at
http://www.smokeylake.com/excel/textfiles.htm. It describes exactly what's happening in
your case, and why it's necessary
 

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