Closed Thread
Results 1 to 9 of 9

Runtime error 9 "subscript out of range"

Hybrid View

  1. #1
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: runtime error 9 "subscript out of range"

    That error translates into: "some object you tried to access by name cannot be found".

    So, your code tried to access a workbook in a folder it couldn't find. Or a sheet in a workbook it couldn't find, or something like that. Spelling always counts when addressing commands to objects.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  2. #2
    Registered User
    Join Date
    05-07-2012
    Location
    Rehoboth Beach, Delaware
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: runtime error 9 "subscript out of range"

    So where is the error??

    Dim wb As Workbook
        Dim wbMEgdF As Workbook, wbMEgdB As Workbook ' don't be concerned about these names.
        Dim rH As Range, rF As Range
        Dim sFileName As String
        Dim LenH As Long, LenF As Long
        Application.ScreenUpdating = False ' turn off the screen updating
         'Show the open dialog and pass the selected _
        file name To the String variable "sFileName"
        sFileName = Application.GetOpenFilename
         'They have cancelled.
        If sFileName = "False" Then Exit Sub
        Set wb = Workbooks.Open(sFileName)
        If InStr(1, ThisWorkbook.Name, "megdf", vbTextCompare) Then
           Set wbMEgdF = ThisWorkbook
           Set wbMEgdB = wb
        Else
           Set wbMEgdF = wb
           Set wbMEgdB = ThisWorkbook
           MsgBox "If here in the code, the code is not in the expected workbook, quitting"
           Exit Sub
        End If
        LenH = wbMEgdF.Worksheets("Data&Parms").Range("B1").Value  ' column H of Data&Parms of wbMEgdF
        LenF = wbMEgdF.Worksheets("Data&Parms").Range("B3").Value   ' column F of Output of wbMEgdF
        If LenH = 0 Then
           MsgBox "No entry in MEgdF!Data&Parms.B1 length of data in Data&Parms"
           Exit Sub
        End If
        If LenF = 0 Then
           MsgBox "No entry in MEgdF!Data&Parms.B3 - length of data in output"
           Exit Sub
        End If
        Set rH = wbMEgdF.Worksheets("Data&Parms").Range("H1").Resize(LenH, 1)
        Set rF = wbMEgdF.Worksheets("Output").Range("F1").Resize(LenF, 1)
        rF.Copy
        wbMEgdB.Worksheets("Data&Parms").Range("H1").PasteSpecial xlValues
        rH.Copy
        wbMEgdB.Worksheets("Output").Range("G1").PasteSpecial xlValues
        wbMEgdB.Save  ' making a guess you want to save wbMEgdB.xls
        wbMEgdB.Close
        Set wb = Nothing ' free memory
        Set wbMEgdB = Nothing
        Application.ScreenUpdating = True ' turn on the screen updating
        MsgBox "data transfer complete"
    End Sub
    Last edited by JBeaucaire; 04-08-2020 at 10:05 PM.

Closed Thread

Thread Information

Users Browsing this Thread

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

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