Removing extra quotation marks from excel exported to text

I need to incorporate a process to remove the extra quotation marks generated by excel when exporting to a text document. I have several lines of text that include quotation marks intentionally, I want to keep these quotation marks unfortunately excel likes to add extras. The existing code to do the export is:

Sub SaveAsText()
   Dim wks As Worksheet
   Dim path As String
   Application.ScreenUpdating = False
   path = "H:\Flags\"
   Set wks = Worksheets("Compiled")
   wks.Copy
   ActiveWorkbook.SaveAs path & ThisWorkbook.Sheets("Entry").Range("C2").Value & ".EB", xlTextWindows
   Application.ScreenUpdating = True
End Sub

Open in new window


I was fortunate enough to be helped along with this bit by rorya.

The other code I've found to remove extra quotations is:
Sub Export()
    Dim r As Range, c As Range
    Dim sTemp As String

    Open "c:\MyOutput.txt" For Output As #1
    For Each r In Selection.Rows
        sTemp = ""
        For Each c In r.Cells
            sTemp = sTemp & c.Text & Chr(9)
        Next c

        'Get rid of trailing tabs
        While Right(sTemp, 1) = Chr(9)
            sTemp = Left(sTemp, Len(sTemp) - 1)
        Wend
        Print #1, sTemp
    Next r
    Close #1
End Sub

Open in new window


I don't really understand how to incorporate this with my existing information, and am not sure if it would work.

Ideally, the end result will be a combined macro to allow me to export based on the first script and keep the original formatting by removing extra quotes, and without requiring any additional data entry.
K_R_MAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dlmilleCommented:
This should work (I just tested it with small sample file) and looks like duplicate " s are removed!

Dave
Sub SaveAsText()
   Dim wks As Worksheet
   Dim path As String, r As Range
   Application.ScreenUpdating = False
   path = "H:\Flags\"
   Set wks = Worksheets("Compiled")
   wks.Copy
   
   Open path & ThisWorkbook.Sheets("Entry").Range("C2").Value & ".EB" For Output As #1
   
   For Each r In ActiveSheet.UsedRange.Rows
        stemp = ""
        For Each c In r.Cells
            stemp = stemp & c.Text & Chr(9)
        Next c
        
        'Get rid of trailing tabs
        While Right(stemp, 1) = Chr(9)
            stemp = Left(stemp, Len(stemp) - 1)
        Wend
        Print #1, stemp
    Next r
    Close #1
   
   'ActiveWorkbook.SaveAs path & ThisWorkbook.Sheets("Entry").Range("C2").Value & ".EB", xlTextWindows
   Application.ScreenUpdating = True
End Sub

Open in new window

dlmilleCommented:
PS - also, there's no need to create a copy of the worksheet - just activate it:

 
Sub SaveAsText()
   Dim wks As Worksheet
   Dim path As String, r As Range
   Application.ScreenUpdating = False
   path = "H:\Flags\"
   Set wks = Worksheets("Compiled")
   'wks.Copy
   
   Open path & ThisWorkbook.Sheets("Entry").Range("C2").Value & ".EB" For Output As #1
   
   wks.Activate
   For Each r In ActiveSheet.UsedRange.Rows
        stemp = ""
        For Each c In r.Cells
            stemp = stemp & c.Text & Chr(9)
        Next c
        
        'Get rid of trailing tabs
        While Right(stemp, 1) = Chr(9)
            stemp = Left(stemp, Len(stemp) - 1)
        Wend
        Print #1, stemp
    Next r
    Close #1
   
   'ActiveWorkbook.SaveAs path & ThisWorkbook.Sheets("Entry").Range("C2").Value & ".EB", xlTextWindows
   Application.ScreenUpdating = True
End Sub

Open in new window


See attached:

Enjoy!

Dave
testoutTxt.xls
test.EB.txt

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
K_R_MAuthor Commented:
That runs very well, does the trick. It does open up a new book in the process. How can I close and not save the new book it creates as part of the steps?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

dlmilleCommented:
It should not be creating any new workbook in the process.  Did you try the latest post - see testoutTxt.xls???

Dave
dlmilleCommented:
that last post commented out the wks.copy command which was creating the new book.  My last post took that out.

Cheers,

Dave
K_R_MAuthor Commented:
That was it, the 2nd version you made had corrected for it! Well done, I can't thank you enough!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.