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
 
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

Hi,, Yes, this worked successfully. Thank you so much for your help..
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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