1

I am an English teacher trying to familiarize myself with VBA. I have the following code below to underline certain words within a text, but I'm having problems with the max. string size of 255.

I want to increase the string size to add more words in a single string (700 to be more precise).

I found this item Getting around the Max String size in a vba function?, but I don't know how to implement that within my code.

    Sub ListChange()
    Dim r As Range
    Dim MyList() As String
    Dim i As Long
    MyList = Split("segregation,investigation,treaty,accommodate,inheritance,tentative,utter,hijack,redundant,disrupt,conserve,lawsuit,graduate,handle,resolution,bewildered,fuss,confident,disguise,considerable,persistent,obligatory,expenses,initially,lecture,reluctance,extinct,toxic,precede,demolition,turnup,gap,disturb,independent,nerve,jusitification,turnout,compulsory,gathering,merciless,HomeOffice,impress,decline,diminish,essentially,deceive,advance,ambiguous,disposeof,disdain,apt,effort,famine,entirely,familiar,elderly,solution,commute,disproportionally,urge,obedience,retreat,merely,relief,distraction,principal,sanity,distortion,figure", ",")
    For i = 0 To UBound(MyList())
       Set r = ActiveDocument.Range
          With r.Find
             .Text = MyList(i)
             .Replacement.Font.Underline = wdUnderlineSingle
             .Execute Replace:=wdReplaceAll
          End With
    Next
    End Sub

I want to get more characters in one string.

Osprey
  • 11
  • 3

1 Answers1

0

I am not sure if String array has any such restriction, although VBA puts a restriction on length of each line. You can continue the same statement by adding an underscore '_' at the end.

Below is a modified version of your subroutine, I could create a long sentence (copy pasted your sentences several times) and was able to generate 700+ words.

Sub ListChange()
    Dim r As Range
    Dim MyList() As String
    Dim i As Long
    MyList = Split("segregation,investigation,treaty,accommodate,inheritance,tentative,utter,hijack,redundant,disrupt,conserve,lawsuit,graduate,handle,resolution,bewildered,fuss,confident,disguise,considerable,persistent,obligatory,expenses,initially,lecture,reluctance,extinct,toxic,precede,demolition,turnup,gap,disturb,independent,nerve,jusitification,turnout,compulsory,gathering,merciless,HomeOffice,impress,decline,diminish,essentially,deceive,advance,ambiguous,disposeof,disdain,apt,effort,famine,entirely,familiar,elderly,solution,commute,disproportionally,urge,obedience,retreat,merely,relief,distraction,principal,sanity,distortion,figure,welcome,world,hello,modification,activedocument,microsoft,hours,terminate,five,six,retinaeye,display,english,increase, " _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, tw0" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, days" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, days" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, days" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, days" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, days" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, days" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, stack" _
& "segregation , investigation, treaty, accommodate, inheritance, tentative, utter, hijack, redundant, disrupt, conserve, lawsuit, graduate, Handle, resolution, bewildered, fuss, confident, disguise, considerable, persistent, obligatory, expenses, initially, lecture, reluctance, extinct, toxic, precede, demolition, turnup, Gap, disturb, independent, nerve, jusitification, turnout, compulsory, gathering, merciless, HomeOffice, impress, decline, diminish, essentially, deceive, advance, ambiguous, disposeof, disdain, apt, effort, famine, entirely, familiar, elderly, solution, commute, disproportionally, urge, obedience, retreat, merely, relief, distraction, principal, sanity, distortion, figure, welcome, world, hello, modification, activedocument, microsoft, hours, Terminate, five, one", ", ")
    For i = 0 To UBound(MyList())
       Debug.Print (MyList(i))
    Next
    Debug.Print ("No of words found in sentence " & UBound(MyList()) + 1)
End Sub

Note: I am only printing the words (removed a section of your code). Hope this is what you are looking for..

Gro
  • 1,613
  • 1
  • 13
  • 19
  • Thank you, that was insightful. I don't have to create 10 Subs. The goal of this code is to identify words within a text and underline them. This would give students an indication on how many studied words are actually in the text. – Osprey Mar 26 '19 at 06:42
  • Creating long lists of words as demonstrated by @Gro can be a bit tedious and can be problematical to maintain. A better solution which is relatively straightforward would be to create a Table in Word itself in a separate document and then from within your macro read in the individual words to your array/collection/dictionary. – freeflow Mar 26 '19 at 10:48
  • That would be very handy as I want to create something which is useful even less savvy teachers than myself. However, I would prefer Excel. I will go into this. Thank you. – Osprey Mar 26 '19 at 22:02