+ Reply to Thread
Results 1 to 10 of 10

Setting variables in vba ranges

  1. #1
    Registered User
    Join Date
    03-13-2021
    Location
    Melbourne
    MS-Off Ver
    Microsoft 365
    Posts
    12

    Setting variables in vba ranges

    I know there are other ways to set a range in excel

    Range("A1:A10")
    Range(Cells(1,1),Cells(10,1))
    Application.Range(Cells(1,1),Cells(10,1))
    etc

    But I want to know how to set a range with a variable inside it, because I want to use a for loop like this:

    Please Login or Register  to view this content.
    Any help is appreciated, thnks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Setting variables in vba ranges

    How about
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Setting variables in vba ranges

    use an offset
    Please Login or Register  to view this content.
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Setting variables in vba ranges

    If the requirement is to sum the rows 2:5 moving across the five columns E:I at each iteration of the loop then your code works as per #1
    It could be marginally shorter.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Setting variables in vba ranges

    If the requirement is to sum the rows 2:5 moving across the five columns E:I at each iteration of the loop then your code works as per #1
    Only if Sheet1 is the active sheet

  6. #6
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Setting variables in vba ranges

    It could be marginally shorter.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,222

    Re: Setting variables in vba ranges

    Deleted...although a result ... nothing to do with requirement...
    Last edited by sintek; 11-20-2021 at 07:12 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Setting variables in vba ranges

    Quote Originally Posted by Fluff13 View Post
    Only if Sheet1 is the active sheet
    ...which could be overcome by fully qualifying the sheet in the code...

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Setting variables in vba ranges

    ...which could be overcome by fully qualifying the sheet in the code...
    Agreed
    ...........

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,222

    Re: Setting variables in vba ranges

    Non looping solution...
    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] setting variables to copy
    By MikeNZ in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-20-2019, 06:19 AM
  2. Setting Global Variables
    By DanzaNZ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-20-2018, 05:06 PM
  3. Setting variables
    By clueless2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2013, 04:59 AM
  4. How to an IF statement by setting up my own IF variables?
    By simpleonline1234 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2010, 08:05 AM
  5. Setting variables to a value
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2009, 12:12 PM
  6. [SOLVED] Setting several variables at the same time
    By BenderBender in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2006, 04:55 PM
  7. Setting variables to Nothing
    By Matt Jensen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-11-2005, 11:06 PM

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