User form: select all text on text field entry not working

C

Chrisso

All

How come this does not work on my Excel 2003 user form?

Private Sub txtStartTime_Enter()
' ### TODO - does not work!
With Me.txtStartTime
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub

Thanks in advance for any thughts,
Chrisso
 
G

GS

Chrisso wrote on 2/18/2011 :
All

How come this does not work on my Excel 2003 user form?

Private Sub txtStartTime_Enter()
' ### TODO - does not work!
With Me.txtStartTime
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub

Thanks in advance for any thughts,
Chrisso

Try 'SelStart = 1' since 1 is the position of the first character. If
the textbox is empty then naturally nothing will be selected, and so
you might want to wrap your code in an If..Then so execution is
conditional on there being text in that textbox.

If txtStartTime = "" Then Exit Sub '//do nothing if empty field
 
R

Rick Rothstein

If txtStartTime is a textbox, then I'm not sure what to tell you Chrisso...
your code works fine for me. Let's double check one thing though... you do
have that code installed in the UserForm's code module and **not** in a
worksheet code module, right?

Rick Rothstein (MVP - Excel)



"Chrisso" wrote in message

All

How come this does not work on my Excel 2003 user form?

Private Sub txtStartTime_Enter()
' ### TODO - does not work!
With Me.txtStartTime
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub

Thanks in advance for any thughts,
Chrisso
 
R

Rick Rothstein

Try 'SelStart = 1' since 1 is the position of the first character.

No, Garry, 0 is the correct starting point for SelStart... SelStart is the
location of the caret (text cursor) and its starting position is before the
first character which is indexed for it at 0. The help file for SelStart
sort of addresses this in the description section for the Long argument.

Rick Rothstein (MVP - Excel)
 
G

GS

It happens that Rick Rothstein formulated :
No, Garry, 0 is the correct starting point for SelStart... SelStart is the
location of the caret (text cursor) and its starting position is before the
first character which is indexed for it at 0. The help file for SelStart sort
of addresses this in the description section for the Long argument.

Rick Rothstein (MVP - Excel)

Thanks, Rick!
This has always been confusing for me to get right, causing me to
always paly around with it to get the correct result. Your explanation
of the relationship between the caret position and the starting point
clears this up. If I've finally captured the gist of it then starting
point is BEFORE the left side of a particular character position (0
being before 1); start position as used in InStr, for example, is to
the left of the specified character position, based on the 1st
character being 1.
 
R

Rick Rothstein

I always think of it as String function work with the actual characters
themselves... the first character is position 1... whereas the Selxxx
functions work with the positions between the characters. Just to be clear
about this, the "Sel" in the various Selxxx commands is short for
"selection" (referring to selected text). Picture in your mind some selected
characters... they are highlighted and SelStart and SelLength are used in
code to do the selecting of those characters. Let's work with one character
because it is easy to picture, so SelLength is 1 (but SelLength could be
more which just means more characters are selected). Picturing the selected
character, think where SelStart must be... yep, it is located to the left of
the character that was selected... it acts as the anchor point for the
selection... the highlight starts from the SelStart position immediately to
the left of the selected character and a SelLength of 1 says starting from
that SelStart position, sweep across one character and highlight it. So, to
select the first character, SelStart, the anchor point is located before
that first character, at position 0 (it really has to be 0 because I think
things would break down at the end of the text if it were 1). I don't know
if the above is clear or not, but I think if you keep in mind that the Sel
part of the command is short for "selection", it might help.

Rick Rothstein (MVP - Excel)




"GS" wrote in message
It happens that Rick Rothstein formulated :
No, Garry, 0 is the correct starting point for SelStart... SelStart is the
location of the caret (text cursor) and its starting position is before
the first character which is indexed for it at 0. The help file for
SelStart sort of addresses this in the description section for the Long
argument.

Rick Rothstein (MVP - Excel)

Thanks, Rick!
This has always been confusing for me to get right, causing me to
always paly around with it to get the correct result. Your explanation
of the relationship between the caret position and the starting point
clears this up. If I've finally captured the gist of it then starting
point is BEFORE the left side of a particular character position (0
being before 1); start position as used in InStr, for example, is to
the left of the specified character position, based on the 1st
character being 1.
 
G

GS

Rick Rothstein submitted this idea :
I always think of it as String function work with the actual characters
themselves... the first character is position 1... whereas the Selxxx
functions work with the positions between the characters. Just to be clear
about this, the "Sel" in the various Selxxx commands is short for "selection"
(referring to selected text). Picture in your mind some selected
characters... they are highlighted and SelStart and SelLength are used in
code to do the selecting of those characters. Let's work with one character
because it is easy to picture, so SelLength is 1 (but SelLength could be more
which just means more characters are selected). Picturing the selected
character, think where SelStart must be... yep, it is located to the left of
the character that was selected... it acts as the anchor point for the
selection... the highlight starts from the SelStart position immediately to
the left of the selected character and a SelLength of 1 says starting from
that SelStart position, sweep across one character and highlight it. So, to
select the first character, SelStart, the anchor point is located before that
first character, at position 0 (it really has to be 0 because I think things
would break down at the end of the text if it were 1). I don't know if the
above is clear or not, but I think if you keep in mind that the Sel part of
the command is short for "selection", it might help.

Rick Rothstein (MVP - Excel)




"GS" wrote in message
It happens that Rick Rothstein formulated :

Thanks, Rick!
This has always been confusing for me to get right, causing me to
always paly around with it to get the correct result. Your explanation
of the relationship between the caret position and the starting point
clears this up. If I've finally captured the gist of it then starting
point is BEFORE the left side of a particular character position (0
being before 1); start position as used in InStr, for example, is to
the left of the specified character position, based on the 1st
character being 1.

Thanks again, Rick. Not sure if your extended explanation is any more
helpful than the first. I've more or less concluded that the Sel????
functions work with the position BEFORE the character whereas the
String functions work with the position the character is AT in the
string.

The part about Sel meaning 'selection' has always been there for me and
so the position part was what I got confused with. I think it's mostly
because I use the String functions way more than Sel???? and so I've
made a mental "i before e except after c" type of rule: "sel BEFORE pos
AT"!
 

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