VBA to remove certain text from CSV file

smythcounty

New Member
Joined
Jul 29, 2021
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hello, The CSV file I'm importing has annoying characters that are causing issues for my reporting. The program I export from places "-" when there is no data and I would like to remove all "-" characters. Is this possible?

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Here it is: (you have to close the textfile first)

VBA Code:
Sub JEC()
 c00 = "C:\Users\xxx\Downloads\test.txt"
 With CreateObject("scripting.filesystemobject")
    .createtextfile(c00).write Replace(.opentextfile(c00).readall, "-", "")
 End With
End Sub
 
Upvote 0
You can also use Ctrl H to use the replace feature to replace them with nothing, or with code
VBA Code:
ActiveSheet.UsedRange.Replace "-", "", xlWhole, , , , False, False
 
Upvote 0
But not if it's a Text file ".txt" ;)

Ctrl+H always works though
 
Upvote 0
I actually read CSV as TXTo_O
omg
 
Upvote 0
Here it is: (you have to close the textfile first)

VBA Code:
Sub JEC()
 c00 = "C:\Users\xxx\Downloads\test.txt"
 With CreateObject("scripting.filesystemobject")
    .createtextfile(c00).write Replace(.opentextfile(c00).readall, "-", "")
 End With
End Sub
Thank you. I should've clarified the "-"s I need removed are located in Columns F2:FXXX and G2:GXXXX. There are some "-"s in the other columns that I wish to keep, such date separators EX. "08-24-2021".
 
Upvote 0
Maybe you missed this

Also the code will work work regardless of file type once it's been imported into xl. ;)
Thank you. I should've clarified the "-"s I need removed are located in Columns F2:FXXX and G2:GXXXX. There are some "-"s in the other columns that I wish to keep, such date separators EX. "08-24-2021".
 
Upvote 0
How about using the same methodology that Fluff's proposed, but this time replace ",-" with ","?
That would not replace any instances where the "-" is not the first character after each ",".
 
Upvote 0
There are some "-"s in the other columns that I wish to keep, such date separators EX. "08-24-2021".
My suggestion will only remove the - if it's the only content of the cell.
But if you only want it to work on col F & G use
VBA Code:
ActiveSheet.Range("F:G").Replace "-", "", xlWhole, , , , False, False
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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