VBA to Find and replace substrings in Excel cells

Thank you for looking at my question,

We have a csv file that contains report data. Each line of the csv file represents one line of the report with only a comma at the end. There are some commas in the reported text which throws the columnisation of the report out.

For each row in the csv file I need to delete the existing commas, find and replace double spaces "  " with commas and then repeatedly find and replace all double commas ",," with single commas until there are no double commas left.

Any help you can offer will be appreciated
Gary CroxfordOperations Support AnalystAsked:
Who is Participating?
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.

Gary CroxfordOperations Support AnalystAuthor Commented:
I have attached a sample file
naamloos.csv
0
Martin LissOlder than dirtCommented:
Please show me what row 1 should look like when the modifications are done.
0
Martin LissOlder than dirtCommented:
Is this what you want? (Change line 5 to match the location of your file)

Sub ImportCSV()
Dim strLine As String
Dim strOldLine As String

Open "C:\temp\naamloos.csv" For Input As #1
Do While Not EOF(1)
    Line Input #1, strLine
    Do Until Len(strLine) = Len(strOldLine)
        strOldLine = strLine
        strLine = Replace(strLine, ",", "")
    Loop
    strOldLine = ""
    Do Until Len(strLine) = Len(strOldLine)
        strOldLine = strLine
        strLine = Replace(strLine, "  ", ",")
    Loop
    strOldLine = ""
    Do Until Len(strLine) = Len(strOldLine)
        strOldLine = strLine
        strLine = Replace(strLine, ",,", ",")
    Loop
    Debug.Print strLine
Loop

Close
End Sub

Open in new window

0

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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Gary CroxfordOperations Support AnalystAuthor Commented:
Martin,

I have attached what the file should like when complete. In essence I am trying to distill the file down to a properly formatted csv file.
MyExcel-Example.xlsx
0
Gary CroxfordOperations Support AnalystAuthor Commented:
Excellent,

Thank you
0
Martin LissOlder than dirtCommented:
It appears that there's an extra "|" in the data like here (1st character of second "line". I'll be back in a while.

|-<BOM Line (Parent Node)> Pono. Seq Item                                            Description        Item Type      Quantity  Uni,
|    t  Eff CO   Eff Date Exp CO   Exp Date Opr,
0
Martin LissOlder than dirtCommented:
So my code is OK as is?
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.