Couldn't delete tabs using ^t?

E

Ed

I was trying to create a macro to delete a leading tab from a selection. It
worked fine, iterating through each paragraph of the range - except it did
not see the tab at the beginning of a paragraph. I used:
Dim objP As Paragraph
Dim rngThis As Range
Set rngThis = Selection.Range
For Each objP In rngThis.Paragraphs
If objP.Range.Characters(1) = "^t" Then
objP.Range.Characters(1).Delete
End If
Next objP

I resolved it using Asc:
If Asc(objP.Range.Characters(1)) = 9 Then
objP.Range.Characters(1).Delete
End If

What was I missing trying to use "^t"?

Ed
 
H

Helmut Weber

Hi Ed,
length of "^t" = 2.
1st character of "^t" = "^".
But then, IMHO, improvement of the interpreter would be possible,
so that if 1 character = 2 characters would generate a runtime error.
Purists would say no, probably.
 
J

Jay Freedman

Hi Ed,

The ^t is a special symbol or alias used only in the Find.Text and
Find.Replacement.Text strings. It isn't literally present in the document --
what is there is a tab character, ASCII 9. Your second code snippet will
work, as would

If objP.Range.Characters(1) = Chr$(9) Then

or, equivalently,

If objP.Range.Characters(1) = vbTab Then
 
E

Ed

Helmut Weber said:
Hi Ed,
length of "^t" = 2.
1st character of "^t" = "^".

I thought it might be something simple like that - which of course is why I
didn't think of it! But since "^t" works for Find, I figured I might just
have syntax wrong. Thanks for responding, Helmut.

Ed
 
E

Ed

Jay Freedman said:
Hi Ed,

The ^t is a special symbol or alias used only in the Find.Text and
Find.Replacement.Text strings. It isn't literally present in the document --
what is there is a tab character, ASCII 9.

Thanks for the info, Jay. I thought I remembered seeing vbTab somewhere
before, but I couldn't put my hands on it. But I'll tape this one to my
monitor! <g>

Ed
 

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