Remove extra commas in a CSV as source allows manual entry in one of the fields

Danton1970

New Member
Joined
Jun 15, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a dump of a large database in CSV format. One of the fields in the source has a comment section where the user can enter freeform text including commas which corrupts the CSV.


Looking to remove extra commas is a CSV file where there are extra
commas in the file. I think a macro along the lines of the following would fix the CSV,



Data Source

in a "good" row there will be 15 commas.
any extra commas will always by after the 10th comma

for each row count the number of commas as "x".
If the number of commas is greater than 15, let "y" be the number of extra commas ie let y= x-15"
If y>0 remove y commas after the 10th comma.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I don't think using Excel to do this is ideal. Using a scripting language is.

A solution like this VB Script works (tested with the data as shown; VB Script to keep the language close to VBA for simplicity). With minor adjustments it should do the trick for what you are doing:

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileRead = objFSO.OpenTextFile("In.csv", 1)
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFileWrite = objFSO2.OpenTextFile("Out.csv", 2, True)

Do Until objFileRead.AtEndOfStream
    a = Split(objFileRead.ReadLine, ",")
    n = Ubound(a)
    i = 0
    l = ""
    for each x in a
        if i < 10 then
            l = l & x & ","
        elseif n-i>4 then
            l = l & x & " "
        else
            l = l & x & ","
        end if
        i = i + 1
    next
    l = left(l, len(l)-1)
    objFileWrite.write(l & vbCrLf)

Loop

objFileWrite.Close
objFileRead.Close

In.csv
a,b,c,d,e,f,g,h,i,j,"k,l,m,n,o",p,q,r,s
a,b,c,d,e,f,g,h,i,j,ha ha,p,q,r,s
a,b,c,d,e,f,g,h,i,j,"yeah, nah",p,q,r,s

Out.csv
a,b,c,d,e,f,g,h,i,j,"k l m n o",p,q,r,s
a,b,c,d,e,f,g,h,i,j,ha ha,p,q,r,s
a,b,c,d,e,f,g,h,i,j,"yeah nah",p,q,r,s

Save the script as a text file and rename it convert.vbs or suchlike with the In.csv in the same folder - the .vbs should then be executable by double-clicking and produce Out.csv
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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