Closest thing I could think of is to use the Split() function.Sorry Paul, i was not completely clear,
F.i. the source string is 150 characters long, i want to break it up in 4 strings of maximum 40 characters and the break must be on whole words.
Regards
Erwin
pardon my intrusion, but the requirement still isn't clear. a MAX of 40 chars? unless you've got a literal understanding of what's in the entire string now, CJ's solution might not always work.F.i. the source string is 150 characters long, i want to break it up in 4 strings of maximum 40 characters and the break must be on whole words.
Function goAuntieJack()
Const BigString = "The quick brown fox jumps over the lazy dog. Jumpy zebra vows to quit thinking coldy of sex. Bob, Carol, Ted and Alice race forwards to Jason's pink zombie quins."
Dim n As Long, arrStrings, Lastn As Long, strPrune As String
strPrune = BigString & " "
Do Until Len(strPrune) < 41
For n = 1 To Len(strPrune)
If Mid(strPrune, n, 1) = " " Then
If n > 40 Then
If IsArray(arrStrings) Then
ReDim Preserve arrStrings(UBound(arrStrings) + 1)
Else
ReDim arrStrings(0)
End If
arrStrings(UBound(arrStrings)) = Mid(strPrune, 1, Lastn)
strPrune = Mid(strPrune, Lastn + 1)
n = 0
Lastn = 0
Else
Lastn = n
End If
End If
Next
Loop
If Len(strPrune) > 0 Then
If IsArray(arrStrings) Then
ReDim Preserve arrStrings(UBound(arrStrings) + 1)
Else
ReDim arrStrings(0)
End If
arrStrings(UBound(arrStrings)) = strPrune
End If
For n = 0 To UBound(arrStrings)
Debug.Print arrStrings(n)
Next
If Trim(Join(arrStrings, "")) = Trim(BigString) Then
Debug.Print "Hurrah!"
End If
End Function
quite agree - which is why I didn't finish it since there are so many potential issues. Requirement is 4 strings - but perhaps the words are long so you can't split it to 4 strings, it needs more. Or perhaps there are more/less than 150 characters.CJ's solution might not always work.
'''''''''''''''''''''''
' Purpose:
'
' split a string into multiple
' string with Maximum character
' count of intMax for each
' new string. Delimeters like .,/; will be
' counted as 1 character.
'
' Parameters:
'
' pstrText = the text to split
' pintMaxLen = the Maximum length of new string
'
' Return:
'
' A One-based array of strings
'
Public Function SplitIntoArray(ByVal pstrText As String, ByVal pintMaxLen As Integer) As Variant
Dim varReturn() As String
Dim var As Variant
Dim i As Integer, j As Integer
Dim length As Integer
Dim strTemp As String
Dim strOld As String
'* check if there is any text on the passed string
If Len(pstrText & vbNullString) = 0 Then
Exit Function
End If
'* remove extra spaces on the string.
Do Until InStr(pstrText, " ") = 0
pstrText = Replace(pstrText, " ", " ")
Loop
pstrText = Trim(pstrText)
'* split the text into variable
var = Split(pstrText, " ")
'* allocate big array
ReDim varReturn(1 To 50)
For i = 0 To UBound(var)
strOld = strTemp
strTemp = strTemp & var(i)
If Len(strTemp) > pintMaxLen Then
j = j + 1
strTemp = Trim(strOld)
varReturn(j) = strTemp
strTemp = var(i) & " "
Else
strTemp = strTemp & " "
End If
Next
j = j + 1
varReturn(j) = Trim(strTemp)
'* remove extra elements from array
ReDim Preserve varReturn(1 To j)
Erase var
SplitIntoArray = varReturn
End Function
Private Sub test()
Const BigString = "The quick brown fox jumps over the lazy dog. Jumpy zebra vows to quit thinking coldy of sex. Bob, Carol, Ted and Alice race forwards to Jason's pink zombie quins."
Dim var As Variant
Dim i As Integer
var = SplitIntoArray(BigString, 40)
For i = 1 To UBound(var)
Debug.Print var(i)
Next
End Sub
does it rival regex()???This module MIGHT be a little bit of overkill,
Function goAuntie()
Const BigString = "The quick brown fox jumps over the lazy dog. Jumpy zebra vows to quit thinking coldy of sex. Bob, Carol, Ted and Alice race forwards to Jason's pink zombie quins."
Dim n As Long, i As Long, arrStrings, strPrune As String
strPrune = BigString
Do Until Len(strPrune) < 41
If IsArray(arrStrings) Then
ReDim Preserve arrStrings(n)
Else
ReDim arrStrings(n)
End If
arrStrings(n) = Mid(strPrune, 1, InStrRev(strPrune, " ", 41))
strPrune = Mid(strPrune, Len(arrStrings(n)) + 1)
n = n + 1
Loop
If Len(strPrune) > 0 Then
ReDim Preserve arrStrings(n)
arrStrings(n) = strPrune
End If
For n = 0 To UBound(arrStrings)
Debug.Print arrStrings(n)
Next
If Trim(Join(arrStrings, "")) = Trim(BigString) Then
Debug.Print "Hurrah!"
End If
End Function