VBA Help Needed-Removing Hyphens From A Column (Worksheet_Change)

milwphil

Board Regular
Joined
Aug 1, 2010
Messages
120
Good afternoon!

I'm trying to come up with code and keep the same type of method consistent with the spreadsheet that I inherited. I've had a lot of luck with this forum, thanks to JonMo1 and others, with a similar issue and I was hoping I'd have the same luck with this issue:

I'm trying to come up with code to remove 'hyphens' in only one column (column I) in a loop (not the entire worksheet).

To be consistent with the methods, I was playing around with WorksheetFunction.Replace (not even sure that's the best function to use- what about WorsheetFunction.Substitute?), but couldn't figure out how to define the range to column I (named "License").

An example of the loop used in the spreadsheet:
Code:
For Each oCell In Target
    oCell.Value = WorksheetFunction.Trim(oCell.Value)
Next oCell

I was hoping for something like:
Code:
For Each oCell In Target
    oCell.Value = [COLOR=red]WorksheetFunction.Replace "-" with "" (in column I or "License") ?????? -I wrote this just as an example (not that I thought it would work in vba)[/COLOR]
Next oCell
THANKS FOR YOUR HELP IN ADVANCE!
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Cell by cell will be slow, but you can use the "find replace" built into excel in vba with the following line:

Code:
Sub HyphenReplacement()
     Columns("I:I").Replace What:="-", Replacement:=""
End Sub
 
Last edited:
Upvote 0
Try this
Code:
Columns("I").Replace What:="-", Replacement:="", _
        LookAt:=xlPart, SearchFormat:=False, ReplaceFormat:=False
 
Upvote 0
WOW! Thanks for the quick responses!

pboltonchina,
Where would I put in the Worksheet_Change code?:

Columns("I").Replace What:="-", Replacement:="", _
LookAt:=xlPart, SearchFormat:=False, ReplaceFormat:=False

CWatts,
I haven't had a chance to try your method yet, but will let you know my results.

THANKS AGAIN!!!!!
 
Upvote 0
Scratch that last question! I got it!

THIS FORUM IS AMAZING!

pboltonchina... I added your code after a loop in the WorkSheet_Change and it worked amazingly! It's rare that I get things on the first shot. I can screw up the most basic instructions sometimes!

Thank you CWATTS and PBOLTONCHINA for your responses!
:beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

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