Excel VBA loop through every row in Textbox multiline

nd0911

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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,180
Office Version
  1. 365
Platform
  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
      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
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,876
Office Version
  1. 2016
Platform
  1. Windows
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
 

Forum statistics

Threads
1,148,160
Messages
5,745,125
Members
423,927
Latest member
Pra56

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
Top