Excel VBA loop through every row in Textbox multiline

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
I have a multiline textbox (with enable to press the Enter key), and I want to loop through every line and get the full line text.

Please note that the textbox word wrap is enabled and if the new line created by wrapping it will be similar to new line (chr(10)), In other words, I need to grab every line of text as it display on the screen and it doesn't matter if its a new line that created by pressing the "Enter" key or just the text wrapping created a new line.

I need somthing like this pseudo code:

VBA Code:
for each line in textbox
       Debug.Pring line
next
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I normally use Word to do that sort of thing. The key is to set a width.

For you, the first input to the function would be your TextBox1.Value. The second input would be the TextBox1.Width.
VBA Code:
'https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-column-widths, 10=75 pixels
'textbox width is in points.
Function LinesToArr(s As String, dPoints As Double, _
  Optional wFile As String = "")
  Dim a(), strLine As String, I As Long, L As Long
  'Tools > References > Microsoft Word xx.0 Object Library
  Dim wdApp As Word.Application, myDoc As Word.Document
  Dim wClose As Boolean
  
  If wFile = "" Then wFile = ThisWorkbook.Path & "\LinesToArray.docm"
  
  'Tell user what file is missing and exit.
  If Dir(wFile) = "" Then
    MsgBox "File does not exist." & vbLf & wFile, _
      vbCritical, "Exit - Missing LinesToArray File"
  End If
  
  On Error Resume Next
  Set wdApp = GetObject(, "Word.Application")
  If Err.Number <> 0 Then
      Set wdApp = CreateObject("Word.Application")
      wClose = True
  End If
  On Error GoTo 0
  'On Error GoTo errorHandler
  
  With wdApp
    .Application.DisplayAlerts = wdAlertsNone
    
    'Open form file and associate data file
    Set myDoc = wdApp.Documents.Open(wFile, Visible:=True)
    With myDoc
      .Content = s
      With wdApp.Selection.ParagraphFormat
        .LeftIndent = InchesToPoints(0)
        '9.6 = 1" in LinesToArray.docm
        .RightIndent = InchesToPoints(10.6 - dPoints / 72)
      End With
      L = .BuiltinDocumentProperties("Number of Lines")
    End With
    
    With wdApp.Selection
      .HomeKey Unit:=wdStory
      Do
        .EndKey Unit:=wdLine, Extend:=wdExtend
        ReDim Preserve a(0 To I)
        a(I) = .Text
        .MoveDown Unit:=wdLine, Count:=1
        .HomeKey Unit:=wdLine, Extend:=wdExtend
        .MoveLeft Unit:=wdCharacter, Count:=1
        I = I + 1
      Loop Until I = L
    End With
    
    .Application.DisplayAlerts = wdAlertsAll
    myDoc.Close False
    Set myDoc = Nothing
    If wClose Then Set wdApp = Nothing
  End With
  
    GoTo EndNow
errorHandler:
    MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
    
EndNow:
  'Trim trailing chars in last element if it exists.
  s = a(UBound(a))
  'If Right(s, 2) = vbNewLine Then a(UBound(a)) = Left(s, Len(s) - 2)
  If Right(s, 1) = vbCr Then a(UBound(a)) = Left(s, Len(s) - 1)
  LinesToArr = a
End Function
 
Upvote 0
Try this code:
VBA Code:
Sub CopyByLine()

Dim z As Long, x As Long, prvLine As Long
Dim Arr() As String

TextBox1.SetFocus
ReDim Arr(TextBox1.LineCount - 1)
prvLine = 0
For z = 1 To Len(TextBox1.Text)
    TextBox1.SelStart = z
    If Not TextBox1.CurLine = prvLine Then
        Arr(prvLine) = x
        prvLine = TextBox1.CurLine
        x = 0
    ElseIf z = Len(TextBox1.Text) Then
        Arr(prvLine) = x
        prvLine = TextBox1.CurLine
    Else
        x = x + 1
    End If
Next

For z = 0 To UBound(Arr)
    Debug.Print Arr(z)
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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