deleting from within a cell

  • Thread starter Completely Confused
  • Start date
C

Completely Confused

I have a column of 5000 part numbers. In each cell contains a part number.
The part numbers looks like this...

1240-00-101-1234
1240-00-102-1234
1240-00-103-1234

I am trying to delete from each cell the dash -
and the first four numbers of the part number so it will look like this

001011234
001021234
001031234
 
D

Don Guillett

Sub cleanupnumbers()
'preformat range as text or do it in macro
For Each c In Range("a2:a4")
ms = Right(c, Len(c) - 4)
c.Value = Application.Substitute(ms, "-", "")
Next
End Sub
 
J

Jim Thomlinson

Note that this method is going to result in a number not a text string,
unless you have formatted the cells as text in advance as suggested. If it is
left a number it is going to drop the two leading zeros. The code will also
not work if you have option explicit at the top of the module.
Here is the code just tweaked a bit... delete the format you do not want...

Sub cleanupnumbers()
dim c as Range
'preformat range as text or do it in macro
For Each c In Range("a2:a4")
c.numberformat = "@" 'for Text
c.numberformat = "000000000" 'for a number
c.Value = Application.Substitute(Right(c, Len(c) - 4), "-", "")
Next c
End Sub
 

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