0

I want to click my macro repeatedly to replace my selected word with the words in my array.

Here is what I want to do:

  1. If Selection = a word in BeingSArray or BeingArray

  2. Then replace Selection with the first/next word in BeingArray

  3. When I run the macro again, it should replace the new word with the second/next word in BeingArray, and so on (in a loop)

Here is one of my many failed attempts:

With Selection

    BeingSArray("am ", "is ", "are ", "was ", "were ", "be ", "being ", "been ") 'I have a separate list here because, when I highlight a word, I may have the following space highlighted with it. 

    BeingArray("am", "is", "are", "was", "were", "be", "being", "been")

    If .Text = BeingArray(1) or .Text = BeingSArray(1) Then .TypeText = BeingArray(2)

    'Do I need to add a command to select the new word here?

    If .Text = BeingArray(2) Or .Text = BeingSArray(2) Then .TypeText = BeingArray(3) 'and so on

    If .Text = BeingArray(8) Or .Text = BeingSArray(8) Then .TypeText = BeingArray(1)

End With

1 Answer 1

1

To begin with, VBA doesn't have a built-in function for finding the location of an item in an array, so you'll have to write your own. Try this:

Function IndexOf(arr As Variant, value, ByRef found As Boolean) As Integer
    Dim lb As Integer, ub As Integer, i As Integer
    found = False
    If Not IsArray(arr) Then Exit Function
    lb = LBound(arr)
    ub = UBound(arr)
    For i = lb To ub
        If arr(i) = value Then
            found = True
            IndexOf = i
            Exit Function
        End If
    Next
End Function

This function is used as follows:

Dim found As Boolean

'prints 0 and False -- first argument is not an array
Debug.Print IndexOf("test", "test", found) 
Debug.Print found

Dim testArray As Variant
testArray = Array("The", "rain", "in", "Spain", "falls", "mainly", "on", "the", "plain")

'prints 0 and False -- "Portugal" is not in the array
Debug.Print IndexOf(testArray, "Portugal", found)
Debug.Print found

'prints 6
Debug.Print IndexOf(testArray, "on", found)
Debug.Print found 'prints True

Once you have such a function:

Dim BeingArray, text As String, found as Boolean
Dim index As Integer, nextIndex as Integer

BeingArray = Array("am", "is", "are", "was", "were", "be", "being", "been")
text = Trim(Selection.Text) 'this is simpler than creating a separate array
index = IndexOf(BeingArray, text, found)
If found Then
    nextIndex = (index + 1) % UBound(BeingArray)
    Selection.Text = BeingArray(nextIndex)
End If
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I appreciate the help, I understand how you approached this, but it's a little over my head. I would not even know where to begin to create the function. The trim function would really help me out. How would I trim or Ltrim my selection before running all of my if then statements with selection?
@WritePlace I'm storing the trimmed selection text within the text variable, so there's no need to trim/ltrim each time.
@WritePlace RE: IndexOf function -- You can search SO or Google for such a function, but I will post such a function shortly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.