+ Reply to Thread
Results 1 to 8 of 8

Subscript out of range error, not sure why

  1. #1
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    89

    Subscript out of range error, not sure why

    Im trying to make a code that will let me select another excel document, i want to copy a cell, and paste it into my original excel file. This keeps giving various errors, and i cant seem to find the best way to do this. Would appreciate any help. (the code isnt done either, i just dont want to make the rest before i know that this part works.)

    This is my code so far

    Sub test()
    Dim wb As Workbook
    Dim wb2 As Workbook
    Dim vFile As Variant
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlManual

    Set wb = ActiveWorkbook

    vFile = Application.GetOpenFilename("Excel-files,*.xlsx", _
    1, "Select One File To Open", , False)

    If TypeName(vFile) = "Boolean" Then Exit Sub
    Workbooks.Open vFile
    'Set targetworkbook
    Set wb2 = ActiveWorkbook
    wb2.Worksheets("Sheet1").Range("C12").Copy
    Set wb = ActiveWorkbook

    wb.Worksheets("Data").Range("B1").Select ' This step keeps yielding the error
    Selection.PasteSpecial Paste:=xlPasteValues


    End Sub

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Subscript out of range error, not sure why

    It looks to me as though you are trying to copy the C12 on Sheet1 in the vFile workbook to B1 on the Data sheet in vFile. Given that you are using getopenfilename, presumably the file that is open sometimes doesn't have a sheet named Data?

    Maybe try:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Subscript out of range error, not sure why

    is the
    Set wb = activeworkbook
    what makes it not work? i tried replacing that step with
    wb.Sheets("Data").Activate
    but now that step is the one not working

  4. #4
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Subscript out of range error, not sure why

    i do have a sheet called data where i want to paste my data. i tried your code and it gives me "automated error on this step:
    wb.Sheets("Data").Range("B1").Value = wb2.Sheets(1).Range("C12").Value

  5. #5
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Subscript out of range error, not sure why

    Maybe i didnt explain it proper in the start. I have one workbook, where i want to be able to import data to, by clicking a button, allowing me to select a file, and then the needed data will be picked out. On this starting workbook, i have 2 sheets, the overview page where the data will be presented in graphs later, and the data page where i want the data to import to.

    The file that is opened does not have a sheet named data, it has "sheet1" and "sheet2", data is on the workbook that im originally on, and where i want to paste the data.
    Last edited by siggisigg; 06-27-2014 at 04:36 AM.

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Subscript out of range error, not sure why

    In your original code the second set wb = activeworkbook will overwrite the first so both will be copying to the same workbook.

    I just double checked my code and tested it, providing there is a sheet called Data in the active workbook when you run the code then it seems to run ok.

    It might be worth trying:
    Please Login or Register  to view this content.
    which will copy the data into the workbook the code is saved in, rather than the active one.

  7. #7
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Subscript out of range error, not sure why

    that worked! thank you so much. Im sure ill be back with a new thread as soon as the next problem arises :P

  8. #8
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Subscript out of range error, not sure why

    Alright so now i need to make things worse. when i put data into the data sheet, i need it not necessarily to put it in b1, i need to have it in row 1, first available column. suggestions?
    Last edited by siggisigg; 06-27-2014 at 04:52 AM.

+ 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. Runtime Error - Subscript out of Range / Object Error
    By JHRice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2012, 05:14 PM
  3. Defining Array - Runtime error 9, Subscript out of range error
    By MaartenW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2012, 07:32 AM
  4. Runtime Error - Subscript out of range despite On Error statement
    By DoctorG in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2006, 11:05 AM
  5. Subscript out of range error - save copy error
    By bg18461 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2006, 11:53 AM

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