Excel VBA loop through every row in Textbox multiline


Board Regular
Jan 1, 2014
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

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Kenneth Hobson

Well-known Member
Feb 6, 2007
Office Version
  1. 365
  1. Windows
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
        .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
    MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
  '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


Well-known Member
Nov 26, 2020
Office Version
  1. 2016
  1. Windows
Try this code:
VBA Code:
Sub CopyByLine()

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

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
        x = x + 1
    End If

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

End Sub
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Latest member

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