VBA to remove certain text from CSV file

smythcounty

New Member
Joined
Jul 29, 2021
Messages
24
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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
560
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,881
Office Version
  1. 365
Platform
  1. Windows
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
 

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
560
Office Version
  1. 365
Platform
  1. Windows
But not if it's a Text file ".txt" ;)

Ctrl+H always works though
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,881
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Maybe you missed this
The CSV file I'm importing
Also the code will work work regardless of file type once it's been imported into xl. ;)
 

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
560
Office Version
  1. 365
Platform
  1. Windows
I actually read CSV as TXTo_O
omg
 

smythcounty

New Member
Joined
Jul 29, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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".
 

smythcounty

New Member
Joined
Jul 29, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
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".
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,116
Office Version
  1. 365
Platform
  1. Windows
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 ",".
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,881
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,148,020
Messages
5,744,364
Members
423,864
Latest member
GolfingTitan116

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
Top