+ Reply to Thread
Results 1 to 8 of 8

Using Variables to Reference Ranges

  1. #1
    Registered User
    Join Date
    07-03-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Using Variables to Reference Ranges

    Thanks to all who help with this simple matter!

    This is a fairly straight-forward question: When I need to reference ranges that are constantly changing, how do I construct the range in VBA?

    For example: I create a table starting with cell A7. I have a few calculated values in columns B, C, and D (7). I have a macro that when I click a button, it increments cell A7's value by 1 and pastes the value in cell A8. I do this using a + 1 counter variable I call "RowNum". Say I would like a border to appear around the values at the bottom of this range (columns A, B, C, and D). To reference it, I thought I could call it .Range("A7:"A" & RowNum), but this is quite incorrect. What would be the proper way in VBA to reference this sort of range?

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using Variables to Reference Ranges

    Quote Originally Posted by CKRebel View Post
    To reference it, I thought I could call it .Range("A7:"A" & RowNum), but this is quite incorrect.
    why do you think so ?

  3. #3
    Registered User
    Join Date
    07-03-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Using Variables to Reference Ranges

    Because individually I can reference a cell as .Range("A" & RowNum).. When putting it in a range such as the one described above, this doesn't work.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using Variables to Reference Ranges

    Hello CKRebel,

    What is the initial value of cell "A7"?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using Variables to Reference Ranges

    attach please a sample of your file with macro

  6. #6
    Registered User
    Join Date
    07-03-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Using Variables to Reference Ranges

    The initial value of cell A7 is 1. What I'm trying to do is create a border around this range, and then when I click a button to add another number "2" in A8, the border appears below A8:D8

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using Variables to Reference Ranges

    Hello CKRebel ,

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-03-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Using Variables to Reference Ranges

    With a slight modification, I was able to make this function work in my spreadsheet. Thank you very much for your help Leith.

    Marked thread as solved and added to your reputation..

+ 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