How can I delete a space/character from a number of fields?

B

BABs

I have a column that has hyphenated numbers (XX-XX-XX-XXX-XXXX) and I need to
get rid of the hyphens. They are all in the same format so I think if I
could write code to del the 3rd, 6th, 9th, etc characters from a cell, it
would work. I just can't seem to figure the code out. or is there an easier
way?
Thx.
 
R

Rick Rothstein

Why not just select the all the cells and use Excel's Edit/Replace to
replace the hyphens with the empty string (put a hyphen in the "Find what"
field and leave the "Replace with" field empty)?
 
R

Ryan H

There is a very easy way without using code. I will assume you are using
Excel 2007. Just highlight the column with the hyphenated numbers, click the
Find & Select button in the Home tab on the Ribbon, click Replace, in the
Find What field type -, and leave the Replace With field empty, then click
the Replace All button. That should do it for you.

Hope this helps! If so, let me know, click "YES" below.
 
R

Ryan H

If you need code you can use this. Just change the range to fit your
application. Hope this helps! If so, let me know, click "YES" below.

Sub ReplaceHyphens()

Dim MyRange As Range

Set MyRange = Range("A:A")
MyRange.Replace What:="-", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

End Sub
 
J

Jef Gorbach

or if you're looking for a code snippet -


Cells.Replace What:="-", Replacement:="", LookAt:=xlPart, SearchOrder
_
:=xlByRows, MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False
 
B

BABs

This works but changes everything to scientific format or rounds off to the
nearest thousand. Can't figure out how to keep the hundreds, tens and ones.
????
 
R

Ryan H

Select your data, then right click the highlighted selection, select Format
Cells, select number, then choose which decimal place you want. Hope this
helps! If so, let me know, click "YES" below.
 
G

Gord Dibben

After edit>replace change formatting to Number with no DP.

Your example shows 14 digits..........Excel will handle up to and including
15 digits.


Gord Dibben MS Excel MVP
 
B

BABs

I am not using decimals. It changes 13-30-234-1000-2904 to 133023410002000,
rounding off to the thousands. I need it to keep the 2904 at the end. I've
tried formatting the column as text, numbers with different formats, custom,
etc. but they all seem to round and drop the 904 to 000.
Any ideas?
 
B

BABs

And therein lies my problem, thank you. I was lazy with the example, my
column actually has 18 digits! Is there anyway that I can expand to 18
digits?
 
R

Ryan H

I was able to use the Replace method just fine. Try formatting all the cells
to Number with decimal place set to 0. Then run the Replace method. Let me
know if this helps, click "YES" below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top