Do you want to simply report that this input string does not have a space or carriage return?
OR
Do you want to check the length of such an input string (no space or carriage return) and
IF
The length is less/equal your desired length, simply output the string.
Else
If the length is greater than your desired length, then cut the string at your desired length, and output the string fragment.
Repeat with remainder of input string until the entire string has been processed.
Update for consideration:
Here is a small revision to Moke's LoopString code to allow for a long text string with or without a space character.
Code:
' ----------------------------------------------------------------
' Procedure Name: LoopString
' Purpose: To shorten a long string into parts of <= strLen.
' Maintain punctuation and no splitting of words
' Revision: Allow a long text string with/without space character(s)
'
'Moke's comment from https://www.accessforums.net/showthread.php?t=90689&p=531637#post531637
'
'What the code does is replace any double spaces with a single space and replaces any vbcrlf with single spaces.
'Then it loops through the string getting as close to the desired line length without splitting a word in half.
'It uses the space to separate whole words.
'As the length of each line is determined it adds that piece to a collection along with a vbCrLf and
'removes it from the string and the loop continues.
'Lastly it loops through the collection concatenating the new string as the functions result.
'
' Procedure Kind: Function
' Procedure Access: Public
' Parameter strIN (String): Long input string
' Parameter strLen (Integer): Max length to output lines
' Return Type: String
' Author: Moke
' Revision: Jack
' Date: 07-Mar-25
' From Moke https://www.accessforums.net/showthread.php?t=90689&p=531630#post531630
' ----------------------------------------------------------------
Function LoopString(strIN As String, strLen As Integer) As String
Dim K As Variant, strOut As String
Dim strA As String, strB As String, strLeftOver As String
Dim col As New Collection
' replace double spaces to single space
Do While InStr(strIN, " ") > 0
strIN = Replace(strIN, " ", " ")
Loop
' replace carriage returns to single space
Do While InStr(strIN, vbCrLf) > 0
strIN = Replace(strIN, vbCrLf, " ")
Loop
'revised string to adjust/parse
strLeftOver = strIN
' Parse the revised string and add the line segments to the collection col
Do Until Len(strLeftOver) = 0
strA = Mid(strLeftOver, 1, strLen)
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' 7/Mar/25 Revison to allow long text with/without a space character
' If space char exists, split at the space char
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If InStr(strA, " ") = 0 Then
'process nonspace string
'add string fragment to col
col.Add strA
strLeftOver = Mid(strLeftOver, strLen + 1)
Else
If Len(strLeftOver) < strLen Then
col.Add strLeftOver
strLeftOver = Right(strLeftOver, Len(strLeftOver) - Len(strLeftOver))
Else
strB = Mid(strA, 1, InStrRev(strA, " "))
col.Add strB
strLeftOver = Right(strLeftOver, Len(strLeftOver) - Len(strB))
End If
End If
Loop
' Process the lines in the collection col
For Each K In col
'Debug.Print K
strOut = strOut & K & vbNewLine
Next
LoopString = strOut
Debug.Print strOut 'for testing
End Function
Sample test/result:
With a space char
Code:
?loopstring("abcdefghijklmnopqr stuvwxyz",11)
abcdefghijk
lmnopqr
stuvwxyz
Code:
?loopstring("abcdef ghijklmnopqrstuvwxyz",11)
abcdef
ghijklmnopq
rstuvwxyz
Without a space char
Code:
?loopstring("abcdefghijklmnopqrstuvwxyz",9)
abcdefghi
jklmnopqr
stuvwxyz
With multiple spaces(as per original)
Code:
?loopstring("If the length is greater than your desired length, then cut the string.",20)
If the length is
greater than your
desired length,
then cut the
string.