Delete characters in cell

A

AuthorizedUserPF

Hello

I have a cell containing characters in A1
The Characters are right aligned. I want to delete the righmost 6 characters
in the cell.
I am trying something like

Sub Remove_Characters()
Anystring = Range("a1")
MyStr = Right(Anystring,6)
MyStr Delete
End Sub

It isn't working. Am I using Delete in the correct context?

Thanks
I am running Win XP Pro and Excel Office XP
 
R

Robin Hammond

I'm guessing you want something like this.

Sub StripRight()
Dim rngCell As Range
For Each rngCell In Selection
rngCell.Value = Right(rngCell.Value, 6)
Next rngCell
End Sub

Robin Hammond
www.enhanceddatasystems.com
 
A

acw

G'day

Try
Sub Remove_Characters()
Range("a1") = Left(Range("a1").Value, Len(Range
("a1").Value) - 6)
End Sub

Tony
 
T

Tom Lorenzo

AuthorizedUserPF said:
Hello

I have a cell containing characters in A1
The Characters are right aligned. I want to delete the righmost 6 characters
in the cell.
I am trying something like

Sub Remove_Characters()
Anystring = Range("a1")
MyStr = Right(Anystring,6)
MyStr Delete
End Sub

It isn't working. Am I using Delete in the correct context?

Thanks
I am running Win XP Pro and Excel Office XP

No, you aren't using it correctly. Assuming you want to place the shorter
string in cell a1, then try this:

Sub Remove_Characters()
Anystring = Range("a1").value

' Compute current length of string
myLen = Len(Anystring)

' Note: extra variables introduced for illustratation.
' delete the extra variables to simplify the routine

If myLen > 6 Then
' What is length of string six characters shorter?
newLen = myLen - 6

' Set new string to the new, shorter length
newString = Left(Anystring, newLen)
' Note: might want to include the TRIM function for the new string,
depending on how you feel about spaces.

' And save that value in the cell
Range("a1").Value = newString
End If

End Sub

Regards,

- Sox
 

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