+ Reply to Thread
Results 1 to 12 of 12

Getting Run Time Error 9: Sub script out of Range.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-01-2015
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    10

    Getting Run Time Error 9: Sub script out of Range.

    I am trying to compare two columns from two different sheets. I cannot use conditional formatting because then it does allow you to use VBA macros for that cells. Basically, I have my second sheet highlighted in blue and if a match is found in second it will reset the color to none. Below is my code.

    Sub Nav()


    Dim ws1 As Worksheet
    Dim ws2 As Worksheet


    Set ws1 = Worksheets("Controller")
    Set ws2 = Worksheets("Order Conversion")
    For Each i In ws1.Range("B2:B3000")
    For Each C In ws2.Range("A2:A3000")
    If i.Cells.Value = C.Cells.Value Then
    C.Cells.Interior.ColorIndex = xlNone
    End If
    Next i
    Next C
    End Sub

    In the both the sheets I have data till row #500 but I want to get this done by 3000 rows because data is updated everyday in sheet one.

    Any help please.
    Thanks.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Getting Run Time Error 9: Sub script out of Range.

    Please use code tags when posting.

    Maybe?

    Sub navbains()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim i As Long
    
    Set ws1 = Worksheets("Controller")
    Set ws2 = Worksheets("Order Conversion")
    For i = 2 To 3000
        If ws1.Cells(i, "B") = ws2.Cells(i, "A") Then
            ws2.Cells(i, "A").Interior.ColorIndex = xlNone
        End If
    Next i
    End Sub

  3. #3
    Registered User
    Join Date
    04-01-2015
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    10

    Re: Getting Run Time Error 9: Sub script out of Range.

    Quote Originally Posted by JOHN H. DAVIS View Post
    Please use code tags when posting.

    Maybe?

    Sub navbains()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim i As Long
    
    Set ws1 = Worksheets("Controller")
    Set ws2 = Worksheets("Order Conversion")
    For i = 2 To 3000
        If ws1.Cells(i, "B") = ws2.Cells(i, "A") Then
            ws2.Cells(i, "A").Interior.ColorIndex = xlNone
        End If
    Next i
    End Sub
    Hi , it is still giving subscript out of range error.

    thanks

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Getting Run Time Error 9: Sub script out of Range.

    Quote Originally Posted by navbains View Post
    Hi , it is still giving subscript out of range error.

    thanks
    Double check the spelling of your sheets name to ensure they match the actual names in your workbooks. Also are both worksheets in the same workbook?

  5. #5
    Registered User
    Join Date
    04-01-2015
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    10

    Re: Getting Run Time Error 9: Sub script out of Range.

    Quote Originally Posted by JOHN H. DAVIS View Post
    Double check the spelling of your sheets name to ensure they match the actual names in your workbooks. Also are both worksheets in the same workbook?
    Yeah, I checked. They are same.

    Thanks

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Getting Run Time Error 9: Sub script out of Range.

    Quote Originally Posted by navbains View Post
    Yeah, I checked. They are same.

    Thanks

    No they are not the same. John was right, you have a space after order conversion. Also the C loop should be the first in the next statement. See my change.

    So either take the space out after the sheet name or go into the code and add a space after the sheet name and before the ". Then run this code, or John's whichever is your choice. But if you run his you have to do the same thing either fix the sheet name or add the space in the code.

    Dim lr1 As Long, lr2 As Long
    
    Set ws1 = Worksheets("Controller")
    Set ws2 = Worksheets("Order Conversion")
    lr1 = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
    lr2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
    For Each i In ws1.Range("B2:B" & lr1)
    For Each C In ws2.Range("A2:A" & lr2)
    If i.Value = C.Value Then
    C.Interior.ColorIndex = xlNone
    End If
    Next C
    Next i
    End Sub

  7. #7
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Getting Run Time Error 9: Sub script out of Range.

    See if this works for you. If not maybe you can post the sheet.

    Sub Nav()
    
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim i As Range, C As Range
    Dim lr1 As Long, lr2 As Long
    
    Set ws1 = Worksheets("Controller")
    Set ws2 = Worksheets("Order Conversion")
    lr1 = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
    lr2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
    For Each i In ws1.Range("B2:B" & lr1)
    For Each C In ws2.Range("A2:A" & lr2)
    If i.Value = C.Value Then
    C.Interior.ColorIndex = xlNone
    End If
    Next i
    Next C
    End Sub

  8. #8
    Registered User
    Join Date
    04-01-2015
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    10

    Re: Getting Run Time Error 9: Sub script out of Range.

    Hi Skywriter,
    I tried your code but it give me " Invalid Next Control Variable reference" and highlights at Next i.

    Sorry, I am pretty new to this programming world.

    Could you please help.

  9. #9
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Getting Run Time Error 9: Sub script out of Range.

    Quote Originally Posted by navbains View Post
    Hi Skywriter,
    I tried your code but it give me " Invalid Next Control Variable reference" and highlights at Next i.

    Sorry, I am pretty new to this programming world.

    Could you please help.
    Read above the code in Post #3.

  10. #10
    Registered User
    Join Date
    04-01-2015
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    10

    Re: Getting Run Time Error 9: Sub script out of Range.

    Quote Originally Posted by skywriter View Post
    Read above the code in Post #3.
    Hi I have attached the file.

    Thanks
    Nav
    Attached Files Attached Files

+ 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] Script Out of Range Error
    By elevate_yourself in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-28-2014, 12:58 PM
  2. [SOLVED] Script out of Range Error
    By elevate_yourself in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-10-2014, 11:42 AM
  3. run time error 9: script out of range?
    By sanjayrshn.hzb in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 08-04-2014, 05:38 PM
  4. [SOLVED] Run Time Error 9 " Script out of range
    By vaibhav2312 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-10-2013, 08:10 AM
  5. Course Bookings Run-time error '9': Script out of range
    By tomsmart@smart- in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2008, 10:45 PM

Tags for this Thread

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