+ Reply to Thread
Results 1 to 7 of 7

subscript out of Range (Run time error 9), need help

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    subscript out of Range (Run time error 9), need help

    Hi,

    AM having an error "subscript out of Range (Run time error 9)" in this macro

    Sub ss()
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please Choose the RTCM File", _
    FileFilter:="Excel Files *.xls (*.xls),")
    
    If FileToOpen = False Then
        MsgBox "No file specified.", vbExclamation, "Duh!!!" ' Notification that nothing is chosen
        Exit Sub
    Else ' Load the file, copy the first sheet and paste it in active sheet ...
        ThisWorkbook.Activate
        ThisWorkbook.ActiveSheet.Range("A1:Z65536").ClearContents
        Workbooks(FileToOpen).Activate
        lrow = Workbooks(FileToOpen).Sheets("Sheet1").Cells(65536, 1).End(xlUp).Row
        Workbooks(FileToOpen).Sheets("Sheet1").Range("A1:Z" & lrow).Copy
        ThisWorkbook.Activate
        ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial xlPasteValues
    End If
    End Sub
    I highlighted a line, where i get this error.

    Please help

    Thanks
    Last edited by shiva_reshs; 07-05-2013 at 02:32 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: subscript out of Range (Run time error 9), need help

    Try this
    Sub ss()
    Dim filetoopen As Variant
    
    filetoopen = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", Title:="Please open the CSV file")
    If filetoopen = "False" Then
        MsgBox "No file specified.", vbExclamation, "Duh!!!" ' Notification that nothing is chosen
        Exit Sub
    Else
        ' Load the file, copy the first sheet and paste it in active sheet ...
        ThisWorkbook.Activate
        ThisWorkbook.ActiveSheet.Range("A1:Z65536").ClearContents
        Workbooks(filetoopen).Activate
        lrow = Workbooks(filetoopen).Sheets("Sheet1").Cells(65536, 1).End(xlUp).Row
        Workbooks(filetoopen).Sheets("Sheet1").Range("A1:Z" & lrow).Copy
        ThisWorkbook.Activate
        ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial xlPasteValues
    End If
    
    
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

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

    Re: subscript out of Range (Run time error 9), need help

    Hi, shiva_reshs,

    GetOpenFilename gives you the path and the name of the file to open but it doesn´t open the file. So you would to do that prior to activating.

    Instead of
    Workbooks(filetoopen).Activate
    you should rather use
    Workbooks.Open (FileToOpen)
    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
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: subscript out of Range (Run time error 9), need help

    sorry. it works perfectly.

    Thanks

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Try this.
    Sub ss()
    Dim wbSrc As Workbook
    
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please Choose the RTCM File", _
    FileFilter:="Excel Files *.xls (*.xls),")
    
    If FileToOpen = False Then
        MsgBox "No file specified.", vbExclamation, "Duh!!!" ' Notification that nothing is chosen
        Exit Sub
    Else ' Load the file, copy the first sheet and paste it in active sheet ...
       
        ThisWorkbook.ActiveSheet.Range("A1:Z65536").ClearContents
        Set wbSrc = Workbooks(FileToOpen)    
        With wbSrc
           lrow = .Sheets("Sheet1").Cells(65536, 1).End(xlUp).Row
          .Sheets("Sheet1").Range("A1:Z" & lrow).Copy
          ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial xlPasteValues
          .Close False
         End With
    End If
    End Sub
    Last edited by Norie; 07-06-2013 at 10:32 PM.
    If posting code please use code tags, see here.

  6. #6
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: subscript out of Range (Run time error 9), need help

    Thanks.. Both method works

    One small tricks needed though..

    When I choose the file, destination file has 3 sheets and i want the data to be copied from sheet3. But the code picks up sheet1 data. Any help??

    Thanks

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: subscript out of Range (Run time error 9), need help

    Simple - change Sheet1 in this line to Sheet3 -
     .Sheets("Sheet1").Range("A1:Z" & lrow).Copy

+ Reply to 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