Retrieve Textbox Value

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,066
I have a textbox which is a multiline textbox and has 10 lines of text.

My question is, how do I retrieve only the value from line 10 into another textbox, i.e. retrieve the 10th line value of textbox1 into textbox2.

Thanks.:confused::confused::confused:
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I have only ever managed to retrieve textbox content by selecting the text box. This offends the coding gods, so I will burn in eternal hellfire.

Eg:
Code:
ActiveSheet.Shapes("Text Box 1").select
MyVariable = selection.characters.text
As for retrieving only line 10: what defines line 10? Is it contingent on text wrapping, is it like an ordered list preceded by the number 10? Please tell.
 
Upvote 0
Perhaps this code will work to get the 10th line (assuming lines are separated by a SHIFT+ENTER, which is Chr(13)):
Code:
tmpArr = Split(TextBox1.Value, Chr(13))
If UBound(tmpArr) - 1 < 9 Then
    MsgBox "There aren't 10 lines of text in TextBox1!"
    Exit Sub
Else
    TextBox2.Value = Mid(tmpArr(9), 2, Len(tmpArr(9)) - 1)
End If
Assumes you're using base 0, not base 1 in the array. If using base 1, change the 9's to 10's.
 
Upvote 0
Sorry, this is a userform textbox which has 10 lines of text that have been retrieved from cells on a sheet.
I know that you can paste the texbox list into a sheet and then retrieve the 10th line but I wanted an easier solution that remains within the userform as I don't like pasting textbox info to a sheet as the sheet may have data in the specific area.
 
Upvote 0
Code:
TextBox2.Text = Split(Textbox1.Text & String(10, vbCr), vbCr)(9)
If that doesn't work, try vbLf
 
Upvote 0
Sorry, this is a userform textbox which has 10 lines of text that have been retrieved from cells on a sheet

If you mean there are 10 different rows for each entry, you can simply link TextBox2 to the tenth row, i.e. A10 (if there's no heading in Column A).

If the 10 lines are in one cell split with the user pressing Alt + Enter at the end of each line, then mikerickson's clever solution should the trick.
 
Upvote 0
Would it be easier to retrieve from the original cells rather than the textbox? If they have changed in the meantime you could store them in a variable.
 
Upvote 0
For what it's worth, my solution didn't need a sheet at all. It creates a temporary array and uses that to split the values, similarly to mikerickson's but using Chr(13) instead of vbCr (they're the same). If that character isn't used to separate each line of text in the textbox, then try Chr(10) or vbLf as suggested.
 
Upvote 0
Thanks for your help everyone, I have used Mike's example as it is exactly what I am after and I changed the vbcr to vblf, works beautifully.:):):)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top