Results 1 to 14 of 14

Getting subscript out of range error - can't figure out why

Threaded View

  1. #1
    Registered User
    Join Date
    07-23-2014
    Location
    Arizona
    MS-Off Ver
    2007
    Posts
    8

    Getting subscript out of range error - can't figure out why

    Hi everyone,

    New user, and novice when it comes to vba. What I'm trying to do is change the text color of specific keywords in a spreadsheet. For example, I'd like to highlight the text of 'how to' in the following sentence "I want to know how to change text color", so that it looks like this "I want to know how to change text color".
    I came across the following, and it works for only two terms (example, "how to" , "how do I"), but I have a string of about 15 terms I'd like to use, but anything I change over two, I get the subscript out of range error. Any help would be much appreciated! Thanks!

    Option Explicit
    Option Compare Text
    
    Sub test()
        Dim myList, myColor, myPtn As String, r As Range, m As Object, x
        Columns(1).Font.ColorIndex = xlAutomatic
        myList = VBA.Array("Cat", "Mouse")  '<-- add more if needed
        myColor = VBA.Array(vbRed, vbBlue)  '<-- adjust as per myList(use Color value, not ColorIndex)
        myPtn = Join$(myList, Chr(2))
        With CreateObject("VBScript.RegExp")
            .Global = True
            .IgnoreCase = True
            .Pattern = "([\^\$\(\)\[\]\*\+\-\?\.\|])"
            myPtn = Replace(.Replace(myPtn, "\$1"), Chr(2), "|")
            .Pattern = "\b(" & myPtn & ")\b"
            For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
                If .test(r.Value) Then
                    For Each m In .Execute(r.Value)
                        x = Application.Match(m, myList)
                        If Not IsError(x) Then
                            r.Characters(m.firstindex + 1, m.Length).Font.Color = myColor(x - 1)
                        End If
                    Next
                End If
            Next
        End With
    End Sub
    Last edited by FumoSanto; 07-23-2014 at 12:29 PM. Reason: tagging code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Run-time error '9': subscript out of range - error occurs on multiple computers except one
    By BrettE in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2014, 11:19 PM
  2. Defining Array - Runtime error 9, Subscript out of range error
    By MaartenW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2012, 07:32 AM
  3. Runtime Error - Subscript out of range despite On Error statement
    By DoctorG in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2006, 11:05 AM
  4. Subscript out of range error - save copy error
    By bg18461 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2006, 11:53 AM
  5. [SOLVED] Type Mismatch error & subscript out of range error
    By Jeff Wright in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1