excelvbaloopstxt

Loop doesn't clean the quotation marks


I've got this code that loops through a list of text files, opens them in Excel, and cleans the quotation marks from them. However, for some mysterious reason that I don't understand, it doesn't save without the quotation marks. I've tried different ways to clean the quotation marks but it still doesn't work.

I need the code to open other text files and remove the quotation marks from every single one.

Dim pathfile As String
Dim pathway As Workbook
Dim localcode As String
Dim wb As Workbook
Dim wayfile As String
Dim i As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False

wayfile = "pathfile"
Set wb = Workbooks.Open(wayfile)
Range("A1").Select

i = 1

Do While Not IsEmpty(Range("a" & i))
    Math = ActiveCell
    pathfile = "pathfile"
    pathfile = Replace(pathfile, "math", Math)
    Set pathway = Workbooks.Open(pathfile)
    Range("A1").Select

    Do While ActiveCell.Value <> ""
        ActiveCell.Replace What:="""", Replacement:="", _
        LookAt:=xlPart, SearchOrder:=xlByRows, _
        MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        ActiveCell.Offset(1, 0).Select
    Loop

    pathway, saveas fileformat:=xlTextWindows
    pathway.Close
    pathfile = "pathfile2"
    pathfile = Replace(pathfile2, "math", Math)
    Set pathway = Workbooks.Open(pathfile)
    Range("A1").Select

    Do While ActiveCell.Value <> ""
        ActiveCell.Replace What:="""", Replacement:="", _
        LookAt:=xlPart, SearchOrder:=xlByRows, _
        MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        ActiveCell.Offset(1, 0).Select
    Loop

    pathway, saveas fileformat:=xlTextWindows
    pathway.Close
    Set wb = Workbooks.Open(camin)
    Range("A1").Select
    ActiveCell.Offset(1, 0).Select
    i = i + 1
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

wb.Close

MsgBox "ok"

Solution

  • I need the code to open other text files and remove the quotation marks from every single one.

    You really do not need Excel for such a thing. Directly open the text file, replace the text and write it back to the text file. Is this what you are trying?

    Option Explicit
    
    Sub Sample()
        Dim MyData As String
        Dim FlName As String
            
        '~~> Change this to the relevant text file
        FlName = "C:\Users\routs\Desktop\test.txt"
            
        '~~> Open text file in 1 go and read it
        Open FlName For Binary As #1
        MyData = Space$(LOF(1))
        Get #1, , MyData
        Close #1
        
        '~~> Replace double quotes
        MyData = Replace(MyData, Chr(34), "")
            
        '~~> Write back to text file.
        Open FlName For Output As #1
        Print #1, MyData
        Close #1
    End Sub
    

    If you have a list of text files then you can convert the above procedure into a Function.