+ Reply to Thread
Results 1 to 4 of 4

Setting Variable Range in VBA with .cells method

  1. #1
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Setting Variable Range in VBA with .cells method

    So I have a couple lines of code,the first one's giving me trouble and the second one I know is not written correctly. I've run into this error a lot but still don't understand the overarching concept that's giving me trouble and my google searching are less than enlightening. If you can tell me what I need to do to fix my code, great, if you can give some insight into why I run into these errors and can't seem to get a hang for variable ranges in VBA, that'd be even better.

    Here's the couple lines of code:

    Please Login or Register  to view this content.
    And in case a little explanation helps, the first line is supposed to set "mylistend" to being the cell at the lower right hand side of a larger selection. The second line of code is supposed to copy that larger selection from the upper left to the lower right (which is now defined as 'mylistend').

    Oh and the error I'm getting is "Object variable or With block variable not set."

    Thanks in advance, I really appreciate it!

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Setting Variable Range in VBA with .cells method

    A few issues:
    1. You don't need the Range statement in the first line if only working with one cell - Cells returns a Range object.
    2. You set 'mylistend' but then try and use 'listend'
    3. When working with object variables, you need to use Set:

    Please Login or Register  to view this content.
    4. For the second line, you do need Range since you are using a start and end cell as arguments, but you don't need the & because you are not concatenating cell addresses - you are simply using a Range object again:
    Please Login or Register  to view this content.

    Finally, since all your ranges refer to the same sheet, it is neater to use a With...End With block or a worksheet object variable, and qualify all the Range and Cells calls with that object:

    Please Login or Register  to view this content.
    or:

    Please Login or Register  to view this content.
    Does that help?
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: Setting Variable Range in VBA with .cells method

    That helps monumentally! And gives me some insight into With blocks which I have yet to use.
    And as to issue #2, ha, it was all originally listend but I was concerned if that was actually a command somewhere in VBA and was messing things up, guess I forgot to change them all to mylistend.

    Thank you very much!

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Setting Variable Range in VBA with .cells method

    Glad to help.

+ 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] Setting a range of cells with a variable
    By fpt264 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2014, 06:19 PM
  2. [SOLVED] Setting a variable range with a variable sheet name. Hmm?
    By starcraftbud in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-11-2013, 11:46 AM
  3. Replies: 15
    Last Post: 09-10-2013, 05:31 AM
  4. setting a range variable
    By Lazhal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-30-2010, 01:50 PM
  5. Variable for Range method
    By gtmeloney in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2009, 11:29 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