Remove the double quote "" around a string when writing to a csv file?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
To all

I have a macro which read/process data from a worksheet and write it to a csv file. I have noticed that using the command
VBA Code:
Write #1, cell2.Value
to write the data whenever i have a string (say aaa) I get "aaa" .

Is there a way of not having the double quote around each string? If the cell contains a number it 's ok

Thanks in advance

John
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi John,

maybe use

VBA Code:
Print #1, cell2.Value
or explain what method you are using.

Holger
 
Upvote 0
Hi Holger

I simply open a file using

VBA Code:
Open MyFile For Output As #1

then loop through each row of a pre-defined range
'do some validity checks
   'if valid row then loop through each cell of this row
   'and for each cell do
   Write #1, cell2.Value,

'when all done I close the file
Close #1


nothing fancy and works just fine for what I want
the annoying issue is that I get the double quote for all cells containing a string

maybe there is a way of writing entire (selected) range to a csv format? no looping
 
Upvote 0
Hi John,

just replace Write with Print to avoid the quotes, and I use Loops (and specify the formatting to meet certain criteria for the content) for each single column. Maybe copy the range you want to export into a new workbook and save as *.csv (MS-DOS) or *.txt(MS-DOS) (both open without showing quotes in my system).

Holger
 
Upvote 0
Hi John,

maybe use

VBA Code:
Print #1, cell2.Value
or explain what method you are using.

Holger
the Print command does indeed get rid of the "" but I don't get the comma

header 1 header 2 header 3 header 4 header 5
aa2 bb2 <2.3.4.5> 22 23
aa3 bb3 <10:20> 32 33 53
 
Upvote 0
Hi John,

just replace Write with Print to avoid the quotes, and I use Loops (and specify the formatting to meet certain criteria for the content) for each single column. Maybe copy the range you want to export into a new workbook and save as *.csv (MS-DOS) or *.txt(MS-DOS) (both open without showing quotes in my system).

Holger
I have been thinking about this option but want to bottom out the Write/Print to a file 1st
 
Upvote 0
Hi John,

Sample.xlsm
ABCDEFG
1DateCustCountyProdSoldPriceAmount
201.01.2023RobertsNSWPears90315,0013.545,00
301.01.2023RobertsTASOranges33115,004.965,00
401.01.2023SmithQLDMangos29920,005.980,00
501.01.2023RobertsQLDOranges61215,009.180,00
601.01.2023RobertsVICApples90712,5011.337,50
701.01.2023PradeshTASPears10718,001.926,00
801.01.2023RobertsVICApples77012,509.625,00
901.01.2023SmithNTApples22312,502.787,50
1001.01.2023SmithVICOranges13215,001.980,00
1101.01.2023PradeshQLDOranges66915,0010.035,00
1201.01.2023RobertsNSWMangos88120,0017.620,00
1301.01.2023KeeSAPears62418,0011.232,00
1401.01.2023RobertsQLDMangos19320,003.860,00
1501.01.2023SmithSAMangos25520,005.100,00
1601.01.2023KeeQLDMangos620,00120,00
1701.01.2023KeeVICMangos31120,006.220,00
1801.01.2023RobertsNTOranges915,00135,00
1901.01.2023KeeTASApples70612,508.825,00
2001.01.2023KeeNTMangos44120,008.820,00
2101.01.2023KeeWAOranges93615,0014.040,00
Sample Export to Text
Cell Formulas
RangeFormula
G2:G21G2=E2*F2


Some of the columns get exported and reimported by using the following codes:

VBA Code:
Sub ExportToTextCSV()
  
  Dim strText             As String
  Dim strPathFileName     As String
  Dim intFF               As Integer
  Dim lngRow              As Long
  
  Const strVarDel         As String = ";"
  Const strTextDel        As String = """"
  Const strDateDel        As String = "#"
  
  strPathFileName = Application.DefaultFilePath & "\Sample Export.csv"
  
  intFF = FreeFile
  
  Open strPathFileName For Output As #intFF
  lngRow = 2
  
  Do
    With ActiveSheet
      strText = strDateDel & Format(.Cells(lngRow, 1), "yyyy-mmm-dd") & strDateDel & strVarDel
      strText = strText & strTextDel & .Cells(lngRow, 2) & strTextDel & strVarDel
      strText = strText & strTextDel & .Cells(lngRow, 4) & strTextDel & strVarDel
      strText = strText & Format(.Cells(lngRow, 6), "0.00") & strVarDel
      strText = strText & Format(.Cells(lngRow, 7), "0.00")
    End With
  
    Print #intFF, strText
  
    lngRow = lngRow + 1
  
  Loop Until IsEmpty(ActiveSheet.Cells(lngRow, 1))
  
  Close #intFF

End Sub

VBA Code:
Sub ImportFromTextFileCSV()
   
  Dim lngCol              As Long
  Dim varSplit            As Variant
  Dim strText             As String
  Dim strPathFileName     As String
  Dim intFF               As Integer
  Dim lngRow              As Long
  Dim varPart             As Variant
  
  Const strVarDel         As String = ";"
  Const strTextDel        As String = """"
  Const strDateDel        As String = "#"
  
  strPathFileName = Application.DefaultFilePath & "\Sample Export.csv"
  
  intFF = FreeFile
  
  Open strPathFileName For Input As #intFF
  lngRow = 2

  ActiveSheet.Cells.Clear

  Do
    Line Input #intFF, strText
    varSplit = Split(strText, strVarDel)
    lngCol = 1
    
    For Each varPart In varSplit
      Select Case Left(varPart, 1)
        Case strTextDel
          ActiveSheet.Cells(lngRow, lngCol) = Mid(varPart, 2, Len(varPart) - 2)
        Case strDateDel
          ActiveSheet.Cells(lngRow, lngCol) = DateValue(Mid(varPart, 2, Len(varPart) - 2))
        Case Else
          ActiveSheet.Cells(lngRow, lngCol) = varPart
      End Select
      lngCol = lngCol + 1
    Next varPart
    lngRow = lngRow + 1
  Loop Until EOF(intFF)
  
  Close #intFF

End Sub

Holger
 
Upvote 0

Forum statistics

Threads
1,217,070
Messages
6,134,412
Members
449,872
Latest member
Big Jake

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top