+ Reply to Thread
Results 1 to 15 of 15

Script out of Range Error

Hybrid View

  1. #1
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Script out of Range Error

    Hello,

    I have written a block of code to check if a sting appears in an array and if so, to make modifications on the line where a match is found. The Reference range and the array are on two separate worksheets. When I run this on a dummy spreadsheet to test the code, it works fine, however when I add it to my master spreadsheet (which has about 20 tabs, lots of database references, pivot tables and code already developed) I am getting a "Runtime-error '9': Subscript out of range" error. The specific line if code is in BOLD below. I have already checked and double checked that the worksheet names are correct and I am using the "Option Explicit" function to ensure no variables are undefined. Any thoughts? Thanks in advance!

    (ps - the actual excel file has too much sensitive information to post screenshots so I cannot provide)

    Option Explicit
    Sub testing()
    
    Dim b As Variant
    Dim i As Long
    Dim lr As Long
    Dim lc As Long
    Dim a As Range
    
    Application.ScreenUpdating = False
    
    lr = Sheets("Master Query").Cells(Rows.Count, "B").End(xlUp).Row
    
    lc = Sheets("Master Query").Cells(1, Columns.Count).End(xlToLeft).Column
    
    b = Sheets("Master Query").Range("B8:B" & lr)
    
    For i = 2 To lr
      Set a = Sheets("Inputs").Range("C32:C34").Find(b(i, 1), LookAt:=xlWhole)
      If Not a Is Nothing Then
        Sheets("Master Query").Cells(i, 3).Value = 20
        Sheets("Master Query").Cells(i, 4).Value = 30
        Sheets("Master Query").Cells(i, 5).Value = 40
        Set a = Nothing
      End If
    Next i
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by elevate_yourself; 10-10-2014 at 11:02 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Script out of Range Error

    It could mean sheet Inputs does not exist on sheet collections.
    You need to check if it exists and the spelling is correct.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Script out of Range Error

    Hi,

    maybe try
    'Set b = Sheets("Master Query").Range("B8:B" & lr)
    
    For i = 2 To lr
      Set a = Sheets("Inputs").Range("C32:C34").Find(Sheets("Master Query").Range("B" & 6 + i), LookAt:=xlWhole)
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: Script out of Range Error

    Yeah I check that and it does exist and the spelling is correct. Any other thoughts?

  5. #5
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: Script out of Range Error

    Hi HaHoBe,

    That fix stopped the error! However, is there a way to keep the 'b' variable part of the code? I will be using this same setup in other instances and so I would like to keep the array as the reference in the .Find

    Thanks!

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Script out of Range Error

    Hi, elevate_yourself,

    you can work with Range and two or three variables or use cells and have the same (only switching the place for rows and columns)
    Sub one()
    Dim lngCtr As Long
    
    Const cstrCOL As String = "B"
    Const clngSTART As Long = 6
    For lngCtr = 1 To 6
      Debug.Print Range(cstrCOL & clngSTART + lngCtr).Address
    Next lngCtr
    End Sub
    You may pass the column string or number as a parameter to the sub as well if you want to (if you use a number for the column make sure to use Cells instead of Range).

    BTW: we don´t see what´s in your worklbook - why do you use find and not WorksheetFunction.CountIf?

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: Script out of Range Error

    Also - now that im using that new code, the cells that get matched are not actually updating to the new values...

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Script out of Range Error

    I suspect, you do not have data in column B. The array b is empty.
    b = Sheets("Master Query").Range("B8:B" & lr)
    Last edited by AB33; 10-10-2014 at 11:25 AM.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Script out of Range Error

    Hi, AB33,

    lr stands the last used row in Column B, Area starts at Row 8 while Loop starts at row 2 - there might a good point no data being available due to different sizes.

    Ciao,
    Holger

  10. #10
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: Script out of Range Error

    @HaHoBe - sorry im pretty new to VBA, and really dont understand what your code is doing.

    @AB33 - I have checked the data in column B and there is lots of stuff there

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Script out of Range Error

    Use this line

    For i = 2 To UBound(b, 1)
    Instead of

    For i = 2 To lr

  12. #12
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: Script out of Range Error

    Doh - that was totally it! It was supposed to start on i = 8. Sorry. I changed that line and updated the range finder in HahoBe's code and now its working great! I would still like to be able to use the array if possible but this works so thats awesome! Thank you both!!

    For i = 8 To lr
      Set a = Sheets("Controls & Inputs").Range("C32:C34").Find(Sheets("ODS-SQL Master Query").Range("B" & i), LookAt:=xlWhole)

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Script out of Range Error

    Hi, elevate_yourself,

    change
    b = Sheets("Master Query").Range("B8:B" & lr)
    to
    b = Sheets("Master Query").Range("B1:B" & lr)
    and you may use your array (which I think isn´t necessary at all).

    Ciao,
    Holger

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Script out of Range Error

    Your code works as per post #11.

  15. #15
    Registered User
    Join Date
    07-18-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: Script out of Range Error

    HaHoBe - thanks! I agree its not. In my many hours of searching trying to find a solution to this problem it seemed like arrays were the only way to fix but clearly thats not the case. Ill be removing the array and just use the change you suggested.

    Thanks again!

+ 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. 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
  2. [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
  3. getting an error- a script out of range - Join function
    By AB33 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2012, 01:29 PM
  4. Script Out Of Range Error at Logon
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2009, 05:52 PM
  5. Script out of range error
    By michele@quality-computing.com in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-24-2005, 05:06 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