Save as Text Adding a Quote Mark " to start of some lines - not sure why

sheepdemon

New Member
Joined
Nov 30, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I have an excel workbook which I'm using to build some SQL. I've gotten it all working beautifully, but now when trying to Save As the sheet with the SQL code on it I am running into issues.

Heres the code I'm using, which works fine :

Excel Formula:
Sub Export_Imported_Sheet_As_CSV()
    Application.DisplayAlerts = False
    Dim Sht As Worksheet
    Dim Filename As String
    Worksheets("Enter Data Here").Activate
    Filename = Range("C6").Value
    Set Sht = Worksheets("SQL Script")
    Sht.Copy
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Filename & ".sql", FileFormat:=20
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
End Sub

This saves the file in "Windows Text" file format, I've tried Windows MS-Dos too. The file exports fine and is created, however some lines have quote marks at the start, others do not. I'm not sure what is causing some to have and others not. Example :

1674044137928.png
1674044165413.png


These lines, when exported to txt, are fine, and produced exactly as they look in the Excel Doc.

1674044250649.png
1674044283230.png


This next selection though - lines 3 and 4 have a " added to the start when exported to TXT, and I've no idea why.

Any ideas why this is occuring, and how to prevent it from occurring?
 

Attachments

  • 1674044233585.png
    1674044233585.png
    10.5 KB · Views: 5

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
It will be due to the presence of certain characters in those lines - eg commas or quote marks.

You could save it as formatted text (space delimited)?
 
Upvote 0
Solution
I suppose I could rewrite the code to pop up a "Save As" prompt as well, but again I'd prefer that to have the correct filetype pre selected if possible, so same question comes up.
 
Upvote 0
It's xlTextPrinter. You'll probably need to rename the file afterwards as I suspect it will add a .prn extension to the name even if you supply an extension.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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