morad medo
New Member
- Joined
- Jun 7, 2019
- Messages
- 4
I am using the following UDF to split a long string into rows of predefined number of words in based on input incell "D3".
Public Function SplitOnNth(ByVal inputStr$, ByVal StartPos&, ByVal NumWords&) As String
Dim arr() As String, i As Long, newArr() As String
arr = Split(inputStr)
ReDim newArr(NumWords - 1)
'Arrays are zero-based, but your string isn't. Subtract 1
For i = StartPos - 1 To StartPos + NumWords - 2
If i > UBound(arr) Then Exit For 'Exit if you loop past the last word in string
'ANYTHING IN PARENTHESES SHOULD BE SKIPPED IN WORDS COUNT BUT INCLUDED IN THE WORDS SPLIT
newArr(i - StartPos + 1) = arr(i)
Next
SplitOnNth = Join(newArr, " ")
End Function
For my cells setup please see attached image:
Depending on the value in cell "D3" (in this case 4) the string is split perfectly into rows but because the string has some numbers in parentheses (which could be anywhere in the string) these should be skipped and not be counted in the splitting of the string. These numbers in parentheses, however, should be part of the final result in their exact position as depicted by the image attached. the drawing on the image attached shows the expected result 4 words in each row plus whatever number in the parentheses.
Your help is much appreciated and thanks in advance.
Public Function SplitOnNth(ByVal inputStr$, ByVal StartPos&, ByVal NumWords&) As String
Dim arr() As String, i As Long, newArr() As String
arr = Split(inputStr)
ReDim newArr(NumWords - 1)
'Arrays are zero-based, but your string isn't. Subtract 1
For i = StartPos - 1 To StartPos + NumWords - 2
If i > UBound(arr) Then Exit For 'Exit if you loop past the last word in string
'ANYTHING IN PARENTHESES SHOULD BE SKIPPED IN WORDS COUNT BUT INCLUDED IN THE WORDS SPLIT
newArr(i - StartPos + 1) = arr(i)
Next
SplitOnNth = Join(newArr, " ")
End Function
For my cells setup please see attached image:
Depending on the value in cell "D3" (in this case 4) the string is split perfectly into rows but because the string has some numbers in parentheses (which could be anywhere in the string) these should be skipped and not be counted in the splitting of the string. These numbers in parentheses, however, should be part of the final result in their exact position as depicted by the image attached. the drawing on the image attached shows the expected result 4 words in each row plus whatever number in the parentheses.
Your help is much appreciated and thanks in advance.