PDA

Click to See Complete Forum and Search --> : [resolved] erasing last 2 characters of a cell value


LodBot
Nov 5th, 2003, 05:46 PM
Could someone please code me an algorythm to erase the last 2 characters in a cell value?

EX: cell A1 has this: "hello world". I want cell A1 to have "hello wor"

How would I go about doing this? Snippets would be nice.

Thanks

RobDog888
Nov 5th, 2003, 10:23 PM
Use the Mid and Len functions like so...
A1 = Mid(A1, 1, Len(A1) - 2)

LodBot
Nov 7th, 2003, 03:31 PM
rowCount = 2
While (Range("C" & rowCount).Value <> "")
Range("C" & rowCount).Value = Mid(Range("C" & rowCount).Value, 1, (Len(Range("C" & rowCount).Value) - 2))
rowCount = rowCount + 1
Wend

When I run this code, I get an error saying "Run Time Error '5': Invalid procedure call or argument"

I'm coding in Excel if it matters.

Thanks

RobDog888
Nov 7th, 2003, 03:39 PM
It looks like you may need to trap for when the cell only has 1 or
2 characters. When you subtract 2 from 1 or 2 you get a negative
number. Invalid Procedure call or argument.
rowCount = 2
While (Range("C" & rowCount).Value <> "")
If Range("C" & rowCount).Value > 2 Then
Range("C" & rowCount).Value = Mid(Range("C" & rowCount).Value, 1, (Len(Range("C" & rowCount).Value) - 2))
Endif
rowCount = rowCount + 1
Wend

LodBot
Nov 7th, 2003, 03:48 PM
problem solved :)

Thanks for the quick reply