+ Reply to Thread
Results 1 to 8 of 8

Error 1004 with Range(Cells(...

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    221

    Error 1004 with Range(Cells(...

    OK, this is probably a simple problem, but I can't figure it out. I set a range variable (WeeklyDates) to a single row of "week ending" dates at the top of a table (cells D1:BU1). I want to copy a 6-month block of "week ending" dates, ending with a user-specified date (Input_EndDate, which is a named range in ThisWorkbook). All the dates are Friday dates. Macro is running in ThisWorkbook (which is the active workbook); the WeeklyDates range is in another (open) file. Here is some of my code:
    Please Login or Register  to view this content.
    WeeklyDates gets correctly set as D1:BU1 on the sheet containing the data. EndCol is correctly assigned as 69 (Input_EndDate matches the value in BT1 of the data sheet). StartCol is correctly set to 44. But I get an Error 1004 when I try to execute the Range.Copy command. What gives? WeeklyDates is a valid range object (one row by 70 columns). Cells(1,44) and (1,69) are within that range. So why is it bombing out? What am I missing here?

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Error 1004 with Range(Cells(...

    VBA does not like that!!

    Replace
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    Even if that does not give you what you want - the syntax is acceptable to VBA - amend to match what you want
    Last edited by kev_; 04-10-2018 at 12:54 PM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    221

    Re: Error 1004 with Range(Cells(...

    Why doesn't VBA like my original code? Doesn't it fit the Range(Cells(row1,col1),Cells(row2,col2)) syntax, since WeeklyDates is declared as a range?
    And if the snytax is wrong, why didn't it throw an error at compile time?

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Error 1004 with Range(Cells(...

    Quote Originally Posted by Merf View Post
    Doesn't it fit the Range(Cells(row1,col1),Cells(row2,col2)) syntax, since WeeklyDates is declared as a range?
    No- a Range object is not the same as the Range property.

    And if the snytax is wrong, why didn't it throw an error at compile time?
    The syntax is OK, since the two cell arguments you pass could contain numbers, in which case something like weeklyDates(1, 2) would be perfectly acceptable.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    221

    Re: Error 1004 with Range(Cells(...

    OK, now I'm REALLY confused.
    WeeklyDates is a range object, correct? I had originally tried:
    WeeklyDates.Range(Cells(1, StartCol), Cells(1, EndCol)).Copy
    (which looks to me like it should be using the range property of the WeeklyDates range object, right?), but that ALSO gives an Error 1004.
    And now you're saying that WeeklyDates(44,69) (in my example) would work? Where is that described in the VBA documentation? I can't find anything that describes a Range syntax like that. I thought you had to use the Cells property (when using row and column numbers, rather than an A1-style reference).

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Error 1004 with Range(Cells(...

    If you qualify the Range property with an object (Worksheet or Range) then you should qualify the Cells property with the same object.

    WeeklyDates(44,69) is shorthand for WeeklyDates.Cells(44,69), which in turn is really shorthand for WeeklyDates.Cells.Item(44,69).

  7. #7
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    221

    Re: Error 1004 with Range(Cells(...

    OK, that makes a little more sense! Thanks!
    Is there a shorthand method for referencing an area (more than one row and column) within a range object that denotes a large table? For example, if MyTable is a range object (variable), how would I denote the range of Cells(2,1) to Cells(5,5) within MyTable? I guess the long way would be:
    Please Login or Register  to view this content.
    Is there a shorthand substitute?

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Error 1004 with Range(Cells(...

    Yes
    Please Login or Register  to view this content.

+ 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] Error 1004 when using Range(Cells(1,1),Cells(2,2)) format.
    By TheClaw2323 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2016, 10:16 AM
  2. [SOLVED] Range(Cells(a,b),Cells(c,d)) Only Works if Sheet is Active?? (Error 1004)
    By Merf in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-15-2015, 06:03 PM
  3. [SOLVED] Error 1004: Application-defined or object-defined error on Range(Cells(x,y)) syntax
    By winch in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-04-2013, 11:02 AM
  4. Error 1004 when selecting a range of cells
    By isrisian in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-18-2011, 07:01 PM
  5. range(cells(),cells()) from 2 different workbooks, run time error 1004
    By sam0287 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2010, 05:46 AM
  6. Worksheet.Range(Cells(r,c),Cells(r,c)) & error 1004
    By onebir in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2007, 05:10 AM
  7. [SOLVED] Charting - Cells and Range - Error 1004
    By vbaprog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2005, 04:06 AM

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.6.0 RC 1