Test for and Remove first character from ActiveCell

A

Andy

I thought this would be easy to figure out, but I'm going in circles.

What would the code be to test the first character in the ActiveCell,
and if true, delete that character?

For example, say I want to remove the first character if it is a "z",
so if the original text is za1234, the new text becomes a1234.

This would be easy with a formula and a helper cell, but I'd like to
be able to do it with code.


TIA,

Andy
 
J

Jean-Guy Marcil

Andy was telling us:
Andy nous racontait que :
I thought this would be easy to figure out, but I'm going in circles.

What would the code be to test the first character in the ActiveCell,
and if true, delete that character?

For example, say I want to remove the first character if it is a "z",
so if the original text is za1234, the new text becomes a1234.

This would be easy with a formula and a helper cell, but I'd like to
be able to do it with code.

What is a "helper cell"?

To remove the "z" only in the active cell, try this:
'_______________________________________
Sub OneCell()

Dim myCell As Cell
Dim CellRge As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Selection must be in a table cell." _
, vbExclamation, "Not in table"
Exit Sub
End If

Set myCell = Selection.Cells(1)
With myCell
Set CellRge = .Range
With CellRge
If LCase(.Characters(1)) = "z" Then
.Characters(1).Delete
End If
End With
End With

End Sub
'_______________________________________

To check all cells in the selected table, try this:
'_______________________________________
Sub AllCell()

Dim TableRge As Range
Dim myCell As Cell
Dim CellRge As Range
Dim i As Long

If Not Selection.Information(wdWithInTable) Then
MsgBox "Selection must be in a table cell." _
, vbExclamation, "Not in table"
Exit Sub
End If

Set TableRge = Selection.Tables(1).Range

With TableRge
For i = 1 To .Cells.Count
Set myCell = .Cells(i)
With myCell
Set CellRge = .Range
With CellRge
If LCase(.Characters(1)) = "z" Then
.Characters(1).Delete
End If
End With
End With
Next
End With

End Sub
'_______________________________________

By the way, this will remove "z" and "Z". If you want to be case specific,
remove LCase(...) from
If LCase(.Characters(1)) = "z" Then
to obtain
If .Characters(1) = "z" Then
and put the "z" or "Z" after the "=".

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Andy

Oh no! I put this message in the wrong section - I need an Excel
macro! (Hence the reference to a helper cell)

Sorry Jean-Guy

Merci,

Andy
 
J

Jean-Guy Marcil

Andy was telling us:
Andy nous racontait que :
Oh no! I put this message in the wrong section - I need an Excel
macro! (Hence the reference to a helper cell)

Sorry Jean-Guy
In this case, trythis:

'_______________________________________
Dim CellText As String

CellText = ActiveCell.Text

If Mid(CellText, 1, 1) = "z" Then
CellText = Right(CellText, Len(CellText) - 1)
End If

ActiveCell.Value = CellText
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Andy

Thanks Jean-Guy.

I needed to think about which text I wanted, as opposed to what I
didn’t' want (delete).


Andy
 

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