VBA Multiple Line handling with Split()


Has

Joined
Jul 12, 2018
Messages
2
Reaction score
2
Hey,
I am trying to create a macro script which can read text within a cell of a table in word. I need to first store the current text of the cell in a string. Then using split() function of VBA split at vbLf (new line) or Char(10) and then loop through the array line by line.

Table is like such
-------------------------------------------------------------------
line 1
line 2
line 3
--------------------------------------------------------------------
--------------------------------------------------------------------|
--------------------------------------------------------------------|

following is my script

Code:
Sub test()
Dim x As Variant
Dim Lst() As String
Dim tbl As Table
Dim i As Integer

Set tbl = ActiveDocument.Tables(1)
x = tbl.Cell(1, 1).Range.Text
'x = "Left" & vbCrLf & "Center" & vbCrLf & "Right"

MsgBox x

Lst = Split(x, vbCrLf) '<-- Works on manually entered data refer to line x="Left" & vbCrLf.....
For i = 0 To UBound(Lst)
    'Do Something
    MsgBox Lst(i)
    Next i
MsgBox Lst(0) '<-- did this to check if the split occured 
End Sub
I also tried a different way of handling the issue by passing the text straight to split() function but no luck. (bellow is the code)

Code:
Sub test3()
Dim Lst() As String
Dim tbl As Table

Set tbl = ActiveDocument.Tables(1)
Lst = Split(tbl.Cell(1, 1).Range.Text, Chr(10))
MsgBox Lst(0) '<-- to check if split() worked. 

End Sub
Please help I don't know if I have used a wrong data type or what?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top