VBA-export sheets to .csv without double quotes...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have been using this script successfully to export data from a sheet to csv (.txt):

Code:
Sub CSVExport()


    Dim var1 As String
    Dim var2 As String
    Dim var3 As String
    Dim var4 As String
    
    'Find the last row that contains data
    With Worksheets("Sheet1")
        lLastRow = .Cells(.Rows.Count, "A").End(xlUp).row
    End With
    
    'Setting the name and the path of text file based on workbook path
    sFName = ThisWorkbook.Path & "csvoutput.txt"
        
    'Get an unused file number
    intFNumber = FreeFile
    
    'Create a new file (or overwrite an existing one)
    Open sFName For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=intFNumber]#intFNumber[/URL] 
        
    For lCounter = 1 To lLastRow


     'Read specific data from the worksheet
        With Worksheets("Sheet1")
            var1 = .Cells(lCounter, 1)
            var2 = .Cells(lCounter, 2)
            var3 = .Cells(lCounter, 3)
            var4 = .Cells(lCounter, 4)
        End With
        
        'Write selected data to text file
        Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=intFNumber]#intFNumber[/URL] , var1, var2, var3, var4
        
    'Continue looping until the last row
    Next lCounter
    
    'Close the text file
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=intFNumber]#intFNumber[/URL] 
   
End Sub

However, this script inserts double quotes around each "item" - I need to export the file without double quotes... Is there any way to adapt my above script to do this or will I have to look for a new solution?

If you can point me in the right direction, I'd be most grateful

Thanks
 
Since you're assigning values to var1, var2, var3, and var4 using the Values property of the Range object, not the Text property, you can use the Print statement instead...

Code:
Print #intFNumber , var1 & "," & var2 & "," & var3 & "," & var4

Hope this helps!
 
Last edited by a moderator:
Upvote 0
Solution
Domenic, you are a star! Thanks very much mate :)
 
Upvote 0
Its the
What have you got in your code sameer?
Its the same code I am trying to run in my excel but giving attached error. I need to double quote to all my data in excels including dates also.
I tried with \"@\" .. it worked but changed entire format of excels.. dates changed into m=numbers.
 

Attachments

  • error25oct21.JPG
    error25oct21.JPG
    67.5 KB · Views: 22
Upvote 0
Try this:

VBA Code:
Sub CSVExport()


On Error Resume Next


Application.ScreenUpdating = False


SheetName = ActiveSheet.Name
Sheets("Sheet1").Activate 'replace with correct sheet


    Dim field1 As String
    Dim field2 As String
    Dim field3 As String
    Dim field4 As String
    
    Dim sFName As String
    Dim intFNumber As Integer
    Dim lCounter As Long
    Dim lLastRow As Long
    
    With Worksheets("Sheet1") 'replace with correct sheet
        lLastRow = .Cells(.Rows.count, "A").End(xlUp).Row
    End With
    
    sFName = ThisWorkbook.Path & "csvoutput.txt"
    intFNumber = FreeFile
    Open sFName For Output As #intFNumber
        
    For lCounter = 1 To lLastRow
        With Worksheets("Sheet1") 'replace with correct sheet
            field1 = .Cells(lCounter, 1)
            field2 = .Cells(lCounter, 2)
            field3 = .Cells(lCounter, 3)
            field4 = .Cells(lCounter, 4)
        End With
        Write #intFNumber, field1, field2, field3, field4
    Next lCounter
    Close #intFNumber


    Sheets(SheetName).Activate


    Application.ScreenUpdating = True
  
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub CSVExport()


On Error Resume Next


Application.ScreenUpdating = False


SheetName = ActiveSheet.Name
Sheets("Sheet1").Activate 'replace with correct sheet


    Dim field1 As String
    Dim field2 As String
    Dim field3 As String
    Dim field4 As String
   
    Dim sFName As String
    Dim intFNumber As Integer
    Dim lCounter As Long
    Dim lLastRow As Long
   
    With Worksheets("Sheet1") 'replace with correct sheet
        lLastRow = .Cells(.Rows.count, "A").End(xlUp).Row
    End With
   
    sFName = ThisWorkbook.Path & "csvoutput.txt"
    intFNumber = FreeFile
    Open sFName For Output As #intFNumber
       
    For lCounter = 1 To lLastRow
        With Worksheets("Sheet1") 'replace with correct sheet
            field1 = .Cells(lCounter, 1)
            field2 = .Cells(lCounter, 2)
            field3 = .Cells(lCounter, 3)
            field4 = .Cells(lCounter, 4)
        End With
        Write #intFNumber, field1, field2, field3, field4
    Next lCounter
    Close #intFNumber


    Sheets(SheetName).Activate


    Application.ScreenUpdating = True
 
End Sub
Hi, Thanks for the code. Its running without error but not able to fetch csv or excel with double quotes :(
 
Upvote 0
that code *should* write a file 'csvoutput.txt' into the same folder as your Excel file...
 
Upvote 0
Ah, there was a missing '\' in the output line... try this:

VBA Code:
Sub CSVExport()


On Error Resume Next


Application.ScreenUpdating = False


SheetName = ActiveSheet.Name
Sheets("Sheet1").Activate
'Sheets("ImportExport").Range("A1:E100").Select
'Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete


    Dim field1 As String
    Dim field2 As String
    Dim field3 As String
    Dim field4 As String
    
    Dim sFName As String
    Dim intFNumber As Integer
    Dim lCounter As Long
    Dim lLastRow As Long
    
    With Worksheets("Sheet1")
        lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    sFName = ThisWorkbook.Path & "\csvOutput.txt"
    intFNumber = FreeFile
    Open sFName For Output As #intFNumber
        
    For lCounter = 1 To lLastRow
        With Worksheets("Sheet1")
            field1 = .Cells(lCounter, 1)
            field2 = .Cells(lCounter, 2)
            field3 = .Cells(lCounter, 3)
            field4 = .Cells(lCounter, 4)
        End With
        Write #intFNumber, field1, field2, field3, field4
    Next lCounter
    Close #intFNumber
    
    'Sheets("Sheet1").Cells.Clear


    Sheets(SheetName).Activate


    Application.ScreenUpdating = True
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,798
Messages
6,193,063
Members
453,773
Latest member
bclever07

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