+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    range(cells(),cells()) from 2 different workbooks, run time error 1004

    Hi guys,

    I've been trying to copy values from one range to another, and the ranges need to be dynamic. Normally I use a combination of the 'range' and 'cells' properties, like this:

    Code:
    range(cells(1,1),cells(x,y)).value = range(cells(2,3),cells(a,b).value
    or something similar, and it works fine.

    However, when I try to do this across two workbooks I get a runtime error. I've isolated the piece of code causing the problem as here:

    Code:
    Sub test()
    
    Dim count As Integer
    
    count = 15
    
    Workbooks("Output Library").Sheets("Standard Sheet (2)").Range(Cells(1, 1), Cells(count, 3)).Value = _
    Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Range(Cells(1, 1), Cells(count, 3)).Value
    
    
    End Sub
    The thing that confuses me is that if I change the range references to a1 style then it works fine:

    Code:
    Sub test()
    
    Dim count As Integer
    
    count = 15
    
    Workbooks("Output Library").Sheets("Standard Sheet (2)").Range("A1").Value = _
    Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Range("A1").Value
    
    
    End Sub
    Anyone have any ideas why the second sub works but the first doesn't?

    Thanks, Sam.

    p.s. I realise I could use copy and paste for this, was just wondering why this method didn't work in this instance.
    Last edited by sam0287; 02-22-2010 at 05:35 AM.

  2. #2
    Forum Guru Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: range(cells(),cells()) from 2 different workbooks, run time error 1004

    Hi Sam

    In the first, you need to fully qualify the Cells objects (ie on which sheets/workbooks they appear) as otherwisse by default (in a standard module) they refer to the activesheet (so you have a range on one sheet, but then try to apply cells on another = mismatch):

    Code:
    With Workbooks("Output Library").Sheets("Standard Sheet (2)")
      .Range(.Cells(1, 1), .Cells(count, 3)).Value = _
    Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Range(Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Cells(1, 1), Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Cells(count, 3)).Value
    End With
    Note the addition of the periods before th Cells on the left hand side of the statement, and the "Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim")." before the Cells on the right hand side - both are fully qualifying the Cells property.

    Richard
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: range(cells(),cells()) from 2 different workbooks, run time error 1004

    Brilliant, thanks a lot!

Thread Information

Users Browsing this Thread

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

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.2.0