+ Reply to Thread
Results 1 to 11 of 11

Run Time Error 9 - Subscript out of range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    265

    Run Time Error 9 - Subscript out of range

    Currently getting this Run Time Error With:

    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim c As Range
    
    Application.ScreenUpdating = False
                Set WS1 = ThisWorkbook.Sheets("Tabelle1")
                Set WS2 = Workbooks("C:\Users\JeRi\Desktop\shortage\" & file).Sheets(file1)
                Set Rng1 = WS1.Range(WS1.Range("A5"), WS1.Range("A" & Rows.Count).End(xlUp))
                Set Rng2 = WS2.Range(WS2.Range("A2"), WS2.Range("A" & Rows.Count).End(xlUp))
                For Each c In Rng1
                On Error Resume Next
                Rng2.Find(What:=c).Offset(, 8).Resize(, 7).Copy Destination:=c.Offset(, 11)
                Err.Clear
                Next c
                Set WS1 = Nothing
                Set WS2 = Nothing
                Set Rng1 = Nothing
                Set Rng2 = Nothing
                Application.ScreenUpdating = True
    Any Ideas why??

    Thank You

    JRidge

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Run Time Error 9 - Subscript out of range

    Where are "file" and "file1" defined and what do they contain?


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    265

    Re: Run Time Error 9 - Subscript out of range

    Quote Originally Posted by TMS View Post
    Where are "file" and "file1" defined and what do they contain?


    Regards, TMS
    Path = "C:\Users\JeRi\Desktop\shortage\bom*"
    file = Dir(Path) ' Retrieving the first Filename.
    Do Until file = ""   ' Start the loop.
    file1 = Left(file, InStrRev(file, ".") - 1)

  4. #4
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    265

    Re: Run Time Error 9 - Subscript out of range

    Solved

    Dim WS1 As Worksheet    Dim WS2 As Worksheet
        Dim Rng1 As Range
        Dim Rng2 As Range
        Dim c As Range
    
    
    Application.ScreenUpdating = False
                Set WS1 = ThisWorkbook.Sheets("Tabelle1")
                Set WS2 = Workbooks(file).Sheets(file1)
                Set Rng1 = WS1.Range(WS1.Range("A5"), WS1.Range("A" & Rows.Count).End(xlUp))
                Set Rng2 = WS2.Range(WS2.Range("A2"), WS2.Range("A" & Rows.Count).End(xlUp))
                For Each c In Rng1
                On Error Resume Next
                Rng2.Find(What:=c).Offset(, 7).Resize(, 7).Copy Destination:=c.Offset(, 11)
                Err.Clear
                Next c
                Set WS1 = Nothing
                Set WS2 = Nothing
                Set Rng1 = Nothing
                Set Rng2 = Nothing
                Application.ScreenUpdating = True

  5. #5
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    265

    Re: Run Time Error 9 - Subscript out of range

    My problem is now:

    Dim WS1 As Worksheet    Dim WS2 As Worksheet
        Dim Rng1 As Range
        Dim Rng2 As Range
        Dim c As Range
    
    
    Application.ScreenUpdating = False
                Set WS1 = ThisWorkbook.Sheets("Tabelle1")
                Set WS2 = Workbooks(file).Sheets(file1)
                Set Rng1 = WS1.Range(WS1.Range("A5"), WS1.Range("A" & Rows.Count).End(xlUp))
                Set Rng2 = WS2.Range(WS2.Range("A2"), WS2.Range("A" & Rows.Count).End(xlUp))
                For Each c In Rng1
                On Error Resume Next
                Rng2.Find(What:=c).Offset(, 7).Resize(, 7).Copy Destination:=c.Offset(, 11)
                Err.Clear
                Next c
                Set WS1 = Nothing
                Set WS2 = Nothing
                Set Rng1 = Nothing
                Set Rng2 = Nothing
                Application.ScreenUpdating = True
    Basically instead of pasting the value 11 columns to the right of c i need to:

    Find the Column with File1 value in.
    The value is then pasted where the c Row and the File1 Column intersect.

    Any Ideas would be greatly appreciated..

    Regards

    JRidge

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Run Time Error 9 - Subscript out of range

    You have the advantage of knowing what your workbook, worksheets and data look like.

  7. #7
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    265

    Re: Run Time Error 9 - Subscript out of range

    Yeah i suppose

    Doesn't help me work out how to code this part though

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Run Time Error 9 - Subscript out of range

    How many choices do you have? One column? Two columns? More than two columns? Lots of columns?

    One thought would be to assign the entire data area to a two dimensional array. You can them loop through the array with x and y co-ordinates until you find file1. Then you can use your x and y co-ordinates as an offset to the base cell address. That'll give you your starting point.

    The loop should be relatively quick regardless of the number of rows/columns as it's all in memory.

    Regards, TMS

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Run Time Error 9 - Subscript out of range

    As an example:

    Option Explicit
    
    Sub sfindCell()
    
    Dim vArray, x As Long, y As Long, rng As Range
    
    vArray = Range("C4:I17")
    
    For x = LBound(vArray, 1) To UBound(vArray, 1)
        For y = LBound(vArray, 2) To UBound(vArray, 2)
                If vArray(x, y) = "TMS" Then
                'Debug.Print x, y, vArray(x, y)
                Set rng = Range("C4").Offset(x - 1, y - 1)
                Exit For
            End If
        Next 'y
    Next 'x
    
    If Not rng Is Nothing Then
        rng.Select
    Else
        MsgBox "oops"
    End If
    
    'Stop
    
    End Sub

    Regards, TMS

  10. #10
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    265

    Re: Run Time Error 9 - Subscript out of range

    Got around it by using this:

    TargetCol = ctr
    Get the column number when i paste the name in earlier in the script.


    Then:

    Rng2.Find(What:=c).Offset(, 7).Resize(, 7).Copy Destination:=c.Offset(, TargetCol - 1)
    and it works toooo

    Regards

    JRidge

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Run Time Error 9 - Subscript out of range

    Thank you for sharing.

+ 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 - 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. [SOLVED] Run-time Error '9': Subscript out of Range
    By Veena Anoohya in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-19-2014, 05:06 AM
  3. [SOLVED] subscript out of Range (Run time error 9), need help
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-05-2013, 02:31 PM
  4. Run time error 9 Subscript out of range
    By vijanand1279 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-28-2012, 03:30 PM
  5. Run time error 9 : Subscript out of range
    By JAtz_DA_WAY in forum Excel General
    Replies: 6
    Last Post: 08-29-2005, 03:26 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