1

I'm using the Application.CheckSpelling() method in Excel and some of the cells have strings of more than 255 characters. This causes a type mismatch error in .CheckSpelling(). To get around this, I want to split up the string into chunks of 255 or less. I want to keep the chunks as close to 255 as possible (so there are fewer chunks) and I want to make sure the string is still split with " " as the delimiter. Current, I check to see if the cell is >255 characters. If it isn't, I run .CheckSpelling(). If it is, I split the cell into an array and run .CheckSpelling() on each member of the array. But this means I'm running .CheckSpelling() at least 256 extra times.

I'm banging my head against the wall trying to figure this out. How can I do this?

7
  • Why not split into single words and check them one by one?
    – FunThomas
    Jun 9, 2017 at 10:18
  • I was doing that originally but it seems to be adding a significant amount of time to the process. The number of cells with 255+ characters is relatively low, but checking each word in those cells takes up 2/3 of the time to run the macro.
    – DukeSilver
    Jun 9, 2017 at 10:20
  • 1
    There are a bunch of solutions on this forum and others about using Regular Expressions to split lines at words. Jun 9, 2017 at 10:24
  • @RonRosenfeld regex is possibly overkill for this specific problem. Jun 9, 2017 at 12:03
  • @serakfalcon the point is that there are multiple Solutions already posted in stackoverflow for this problem that could have be found with a simple search Jun 9, 2017 at 12:07

5 Answers 5

1

Below is one possible approach.

Function betterCheckSpelling(input as String) as Boolean
     Dim Words() as String, i as Long
     Dim tempString as String, lenWords as Long

    If Len(input)<=255 Then
         'if the string is short enough return check spelling result
         betterCheckSpelling = Application.CheckSpelling(input)
    Else
        'otherwise use Split on space to generate an array of short strings
        'set the default result of this part of the function to True 
        betterCheckSpelling = True
        Words = Split(input," ")
        lenWords = UBound(Words)
        For i = 0 to lenWords
            'go through each of the short strings and start concatenating together to tempString
            'if the current word would cause tempString to be too long; then check spelling.

            If (len(tempString)+len(Words(i))) > 255 Then
                If Not Application.CheckSpelling(tempString) Then
                      'if the result is False then we don't need to check further; there's already a problem.  
                      betterCheckSpelling = False
                      Exit For
                 Else
                      'otherwise, if the result is True (meaning OK so far) then restart the string from the current word.
                      tempString = Words(i)
                 End If
            Else
                'if the string hasn't reached the full length yet, keep concatenating
                tempString = tempString & " " & Words(i)
            End If
        Next
        If (betterCheckSpelling) Then
            'need to do this one last time for the last piece of tempString
            betterCheckSpelling = Application.CheckSpelling(tempString)
        End If
    End If
End Function

Here is a different, recursive approach.

Function recursiveCheckSpelling(input as String) as Boolean
    Dim checkLength as Integer, test as String
    If Len(input)<=255 Then
        'default, short case.
        recursiveCheckSpelling = Application.CheckSpelling(input)
    Else
        checkLength = InstrRev(input," ",255)
        test = Left(input,checkLength)
        If Application.CheckSpelling(test) Then
            'keep going with next part
            recursiveCheckSpelling = recursiveCheckSpelling(Right(input,Len(input)-checkLength))
        Else
            'failed this level of test; no need to continue
            recursiveCheckSpelling = False
        End If
    End if
End Function
1

You can itterate over the string, chechking if length is longer then 255. If so use Instrrev(string," ",255) to find the location of the space closest to character 255. Split the string at that character using Left(string, number). The remaining string is then given by Right(string, len(string)-number) Then check again if the remaining string is longer then 255 and if needed repeat the proces.

0

For example:

 Dim s As String 'this is the string we'll split

    Dim n As Integer

    n = Len(s)/255
    Dim ArraySplit() As String 'this is where we store our string parts
    Redim ArraySplit(n) 'make sure we can fit every part in
    Dim a As Integer
    a = 0
    Dim b As Integer
    b = 0
    For i = 1 To n
        If Instr(b +1,s,""") < 255 Then  
'make sure we don't get too long piece of the string
            a = Instr(b +1,s,""") 'we get the next location of " in the string
        Else
            a = 255
        End If
        b = b + a
        ArraySplit(i) = Mid(s,b,a) 'we get the next a character from the string '(until we run into ") We use b to store our starting point
    Next i
    'now your string is split by using " as delimiter and no piece is longer than 255 and you can do another loop over ArraySplit() to do what you want to do.
0

I checked, calling Application.CheckSpelling() for every single word is dramatically slow.

You can try something like

Function MySpellCheck(s As String) As Boolean

Dim words() As String, chunk As String, i As Long
words = Split(s, " ")
For i = 0 To UBound(words)
    If Len(chunk) + 1 + Len(words(i)) > 255 Then
        ' Check chunk of data before it gets too long
        If Not Application.CheckSpelling(chunk) Then
            MySpellCheck = False
            Exit Function
        Else
            chunk = ""
        End If
    Else
        chunk = chunk & " " & words(i)
    End If
Next i
' Check last chunk of data
MySpellCheck = Application.CheckSpelling(chunk)

End Function

Not optimal (and fails if there are words longer than 255 chars), but you get the idea

0

I've been using the solution below by Joacim Andersson. It's concise and intuitive to me.

Function SplitString(ByVal str As String, ByVal numOfChar As Long) As String()
    Dim sArr() As String
    Dim nCount As Long
    ReDim sArr((Len(str) - 1) \ numOfChar)
    Do While Len(str)
        sArr(nCount) = Left$(str, numOfChar)
        str = Mid$(str, numOfChar + 1)
        nCount = nCount + 1
    Loop
    SplitString = sArr
End Function

Source

Sample usage: Debug.Print Join(SplitString("abcde", 2), ", ")

Output: ab, cd, e

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.