Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel: Textbox select the last line

Status
Not open for further replies.

gustaf111

Programmer
Jan 16, 2011
87
SE
Hello, a simple question I think ...

How do I select the last line in a multiline textbox from vba ?
 
Not the most elegant but a one liner:
Code:
MsgBox Split(YourTextBox.Value, vbCrLf)(UBound(Split(YourTextBox.Value, vbCrLf)))
Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Be careful, the CurLine fails if set to 0 (first line):
Code:
With Me.TextBox1
    .SelStart = 0
    .CurLine = .LineCount - 1
End With

combo
 
I don't have .CurLine or .LineCount (what version of VBA?) but mine will die if there's no test for NULL values...

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
After finally finding out what combo was talking about (damn Access [wink]) and a bit of tinkering (to add a selection as it's not clear if you want the text, want to be at the last line or select the actual last line) here's what I've got (I'm worryingly starting to get a taste for VBA now after all this time... [tongue])
Code:
With Me.TextBox1
    .SetFocus
    .SelStart = 0
    If .LineCount > 1 Then .CurLine = .LineCount - 1
    .SelLength = 65535
End With
Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
It does not work for me :( I'm using excel, and there is no .SetFocus :(

I tried to súse Select instead with no success :(
 
For a textbox in the worksheet use Activate. Test if it is necessary (no need for SetFocus on the userform).

combo
 
Thanks! I found that out just, but is does not work as expected.

I have textbox that I add/remove height to fit the text, when I have text on the last row, and add one row with CTRL-Enter, then I can access the last row, but if I do not have text on the last row, after I chnaged the height of the textbox I can not access the last row.

Why do I need to press CTRL-Enter to move to the next row ?
 
How do I know exact hight to add to the textbox if I use calibi 11 ?
 
:) I found how to go to the last line if you use CTRL-Enter !

use the Right arrow!! Strange but it works !


Thanks for help me! And take your time!
 
Andy said:
I can't get LineCount or CurLine to work without the Textbox having focus on a userform
You are right, I had it working with one control and commandbutton

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top