+ Reply to Thread
Results 1 to 9 of 9

Run-time error '9': Subscript out of range

Hybrid View

  1. #1
    Registered User
    Join Date
    07-02-2014
    Location
    Branson, MO
    MS-Off Ver
    365
    Posts
    8

    Run-time error '9': Subscript out of range

    [SOLVED]I am trying to run the following code out of my personal workbook. It works on any new workbook I create, but not on any previous workbook (still xlsx) that I open.

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    Sub HighlightRow()
    ' Highlight the entire row of the cell selected
    
    Application.ScreenUpdating = False
     
    ' Remove the Conditional formatting and "screen updating" code from the active sheet (toggle off)   
        For i = 1 To Cells.FormatConditions.Count    ' loops through all conditional formatting rules
            If Cells.FormatConditions(i).Formula1 = "=CELL(""row"")=ROW()" Then
                Cells.FormatConditions(i).Delete
                With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
                    On Error GoTo ExitS
                    .DeleteLines 1, [5]
                End With
    ExitS:
                Exit Sub
                On Error Resume Next
            End If
        Next i
     
    ' if the above rule is not found, inserts rule and "screen updating" code into activesheet (toggle on)   
        Cells.FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=CELL(""row"")=ROW()"
        Cells.FormatConditions(Cells.FormatConditions.Count).SetFirstPriority
        With Cells.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.14996795556505
        End With
        Cells.FormatConditions(1).StopIfTrue = False
        
        With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
            .InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") + 1, _
                         String:=vbCrLf & "    application.screenupdating = True"
        End With
        Application.VBE.MainWindow.Visible = False
    
    Application.ScreenUpdating = True
    
    End Sub
    Sorry if this is a bit messy. Any help would be appreciated.
    Last edited by ndsutherland; 10-27-2014 at 12:27 PM. Reason: add narration

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Run-time error '9': Subscript out of range

    Hello ndsutherland,

    If the workbooks you are opening are xlsx then the code will fail. The reason is an xlsx workbook does not contain VBA macros and therefore no code modules.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-02-2014
    Location
    Branson, MO
    MS-Off Ver
    365
    Posts
    8

    Re: Run-time error '9': Subscript out of range

    Thank you for responding.

    I am running this code out of "personal.xlsb," and it works in any .xlsx file I create, close, and reopen but not on previously created xlsx or xlsm files.
    Last edited by ndsutherland; 10-24-2014 at 09:58 PM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Run-time error '9': Subscript out of range

    What are you trying to do with this code?

    It appears you are trying to alter code in a workbook and apply conditional formatting in the same workbook.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    07-02-2014
    Location
    Branson, MO
    MS-Off Ver
    365
    Posts
    8

    Re: Run-time error '9': Subscript out of range

    This code toggles conditional formatting to highlight the active row and inserts and deletes code in the activesheet module to update every time a new cell is selected.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Run-time error '9': Subscript out of range

    Why do you need to insert/delete code to do that?

  7. #7
    Registered User
    Join Date
    07-02-2014
    Location
    Branson, MO
    MS-Off Ver
    365
    Posts
    8

    Re: Run-time error '9': Subscript out of range

    Without the code I have to manually recalculate every time I select a different cell for the conditional formatting to take effect.
    Last edited by ndsutherland; 10-25-2014 at 11:15 AM.

  8. #8
    Registered User
    Join Date
    07-02-2014
    Location
    Branson, MO
    MS-Off Ver
    365
    Posts
    8

    Re: Run-time error '9': Subscript out of range

    I believe I got it working. I had to do some research on extensibility on the CPearson site. I'm not a big fan of enabling access to the VBA project, but I added a reminder to disable the checkbox.

    Before running this code, enable access to the vba project model and enable microsoft visual basic extensibility 5.3 in references for whichever workbook you run this code out of.

    Sub HighlightRow()
    ' Highlight the entire row of the cell selected
    
    On Error GoTo NextSection
    ActiveWorkbook.VBProject.References.AddFromGuid _
            GUID:="{0002E157-0000-0000-C000-000000000046}", _
            Major:=5, Minor:=3
            
    NextSection:
    Dim CodeMod As VBIDE.CodeModule
    Set cmod = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.ActiveSheet.CodeName).CodeModule
     
    ' Remove the Conditional formatting and "screen updating" code from the active sheet (toggle off)
        For i = 1 To Cells.FormatConditions.Count    ' loops through all conditional formatting rules
            If Cells.FormatConditions(i).Formula1 = "=CELL(""row"")=ROW()" Then
                Cells.FormatConditions(i).Delete
                With cmod
                    On Error GoTo ExitS
                    .DeleteLines 1, [5]
                End With
    ExitS:
    MsgBox ("uncheck ""Trust access to the VBA project object model""")
                Exit Sub
                On Error Resume Next
            End If
        Next i
     
    ' if the above rule is not found, inserts rule and "screen updating" code into activesheet (toggle on)
        Cells.FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=CELL(""row"")=ROW()"
        Cells.FormatConditions(Cells.FormatConditions.Count).SetFirstPriority
        With Cells.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.14996795556505
        End With
        Cells.FormatConditions(1).StopIfTrue = False
        
        With cmod
            .InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") + 1, _
                         String:=vbCrLf & "    application.screenupdating = True"
        End With
        Application.VBE.MainWindow.Visible = False
    
    End Sub
    Last edited by ndsutherland; 10-27-2014 at 02:03 PM.

  9. #9
    Registered User
    Join Date
    07-02-2014
    Location
    Branson, MO
    MS-Off Ver
    365
    Posts
    8

    Re: Run-time error '9': Subscript out of range

    I attached a basic example
    HighlightRow Example.xlsm

+ Reply to Thread

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
    By chergian in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2014, 02:40 AM
  2. [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
  3. [SOLVED] Run Time Error 9 - Subscript out of range
    By kushmakarsharma in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-25-2012, 08:02 AM
  4. VB Run-time error '9'; Subscript out of range
    By lisabethvw in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-29-2009, 03:49 AM
  5. run-time error '9': Subscript out of range
    By AccessHelp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-30-2005, 01:05 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