Count the number of lines in an active x textbox?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,

Is there a vba that can count the number of rows in an active x text box (I have multi rows selected, I want to know how many there are?)


basically if I was writing it in English what I want is

Textbox1 number of lines = ???

please help if you can

thank

Tony
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The new line character for a textbox is denoted by the constant vbCr or its equivalent value Chr(13). Therefore, we can use the Split function to return an array of lines based on vbCr as the delimiter. Then we can use Ubound to return the number of elements in the array, which represents the number of lines. However, since Split() returns a zero-based array, we'll need to add 1 to the result returned by Ubound. So, assuming that Sheet1 contains your ActiveX textbox, we would have the following...

Code:
Ubound(Split(Worksheets("sheet1").TextBox1.Value, vbCr))+1
Change the sheet name accordingly.
 
Last edited:
Upvote 0
Hi Tony,

This should suffice hopefully:

Code:
Sub CountLines()

Dim wb As Workbook
Dim ws As Excel.Worksheet


Set wb = ThisWorkbook
Set ws = wb.Sheets(1)


LineCount = UBound(Split(ws.Shapes("TextBox 1").TextFrame2.TextRange.Characters.Text, Chr(10))) + 1


MsgBox LineCount


End Sub

Please replace TextBox 1 with the name of your TextBox.

Br
pella88
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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