+ Reply to Thread
Results 1 to 5 of 5

Using VBA to Reference a Dynamic Range based on Last Cell of an Adjacent Column

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    3

    Using VBA to Reference a Dynamic Range based on Last Cell of an Adjacent Column

    Hello,

    I am currently trying to refer to a cell range defined as being from Cell K2 down to the same row as the last cell in Column J (e.g. If the last cell in Column J is J1455, the range would be "K2:K1455").

    I have tried using the following syntax but keep getting an error (Runtime Error 1004: Method 'Range' of object '_Worksheet' failed):

    Set rMonthRng = Sheet2.Range("J2", Range("J65536").End(xlUp)).Offset(0, 1)
    I don't understand this as I have used the same structure previously in other workbooks and it worked fine.

    Is anyone able to explain to me what I am doing wrong?

    Thank you!
    Last edited by PW11; 07-23-2014 at 01:39 AM.

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

    Re: Using VBA to Reference a Dynamic Range based on Last Cell of an Adjacent Column

    Hi, PW11,

    try either
    With Sheet2
      Set rMonthRng = .Range("J2", .Range("J" & Rows.Count).End(xlUp)).Offset(0, 1)
    End With
    or
    With Sheet2
      Set rMonthRng = .Range("J2", .Range("J" & Rows.Count).End(xlUp).Offset(0, 1))
    End With
    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

  3. #3
    Registered User
    Join Date
    11-15-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    3

    Re: Using VBA to Reference a Dynamic Range based on Last Cell of an Adjacent Column

    Hi Holger,

    Thanks for the quick response -

    Your options worked perfectly.

    I have incorporated your suggestion, but for my own understanding - are you able to tell me why my code does not work?

    I have used it before and have not had a problem so am totally stumped as to why it wouldn't work.

    Many thanks!

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

    Re: Using VBA to Reference a Dynamic Range based on Last Cell of an Adjacent Column

    Hi, PW11,

    Sheet2.Range("J2",
    This part is related to the sheet with the codename Sheet2 while
    Range("J65536").End(xlUp)
    relates to the ActiveSheet. As long as ActiveSheet is Sheet2 there should be no problem, otherwise the range object would require you to build two areas of this range: one on the ActiveSheet, one on Sheet2.

    HTH,
    Holger

  5. #5
    Registered User
    Join Date
    11-15-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    3

    Re: Using VBA to Reference a Dynamic Range based on Last Cell of an Adjacent Column

    Hi Holger,

    Excellent!

    Thanks very much for this - explains perfectly why it was working in one context, but not here (the code was being run from the active sheet in my original example, but not in this one).

    Very much appreciate the education!

    Kindest Regards

    PW11

+ 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. Dynamic averaging based on adjacent column.
    By projectatpel in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-20-2013, 02:21 PM
  2. [SOLVED] Dynamic averaging based on adjacent column.
    By projectatpel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2013, 12:58 PM
  3. Dynamic range column reference
    By tuna666 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-17-2013, 04:42 AM
  4. Identify a range based on adjacent column....
    By Alex_Sal in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-25-2013, 07:56 AM
  5. Selecting range based on adjacent column
    By yaju1120 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2007, 10:41 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