+ Reply to Thread
Results 1 to 6 of 6

Cell value as variable in a range statement

  1. #1
    Registered User
    Join Date
    01-05-2011
    Location
    Ravenswood, WV
    MS-Off Ver
    Excel 2007
    Posts
    4

    Cell value as variable in a range statement

    First timer. So please excuse the the simpleton.
    I wish to copy a selection with the bottom row number linked to a cell value in the active worksheet. My code reads:

    Sub test()
    '
    ' test Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+J
    '
    Dim ABC As Integer
    ABC = Range("AC4").Value
    Range("B18:X" & ABC).Select
    Selection.Copy
    End Sub


    I read a lot of previous posts and thought I had it right but I still get [Method 'Range' of object'_Global' failed]. Any help would be greatly appreciated.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Cell value as variable in a range statement

    Hi JROG

    Welcome to the Forum

    Please wrap your code in code tags, before the moderators get you...

    Forum rules
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button.

    Cheers

  3. #3
    Registered User
    Join Date
    01-05-2011
    Location
    Ravenswood, WV
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Cell value as variable in a range statement

    REPOST tags. Thanks for the instruction

    First timer. So please excuse the the simpleton.
    I wish to copy a selection with the bottom row number linked to a cell value in the active worksheet. My code reads:

    Please Login or Register  to view this content.
    I read a lot of previous posts and thought I had it right but I still get [Method 'Range' of object'_Global' failed]. Any help would be greatly appreciated.

  4. #4
    Registered User
    Join Date
    01-05-2011
    Location
    Ravenswood, WV
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Cell value as variable in a range statement

    Double please!

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Cell value as variable in a range statement

    Try this. If it beeps, the value in AC4 is out of range.

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    01-05-2011
    Location
    Ravenswood, WV
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Cell value as variable in a range statement

    Shg,

    It beeped. I moved the reference from AC1 to V1 and it worked! Praise VBAllah.

    However, I have no idea why the variable needed to be in the same set of columns as the selection range.

    Anyway, thank you very much. My excel project has taken a giant step forward.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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