+ Reply to Thread
Results 1 to 7 of 7

use a variable within a range name

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2012
    Location
    Banbury, England
    MS-Off Ver
    Excel For Mac 2011
    Posts
    3

    use a variable within a range name

    Hello,
    I have code similar to the following:

    Sub test()
    Set Row1 = Range("A15", "AA17")
    Set Row2 = Range("A18", "AA20")
    Set Row3 = Range("A21", "AA23")
    Set Row4 = Range("A24", "AA26")
    Set Row5 = Range("A27", "AA29")
    Set Row6 = Range("A30", "AA32")
    Set Row7 = Range("A33", "AA35")
    Set Row8 = Range("A36", "AA38")
    Set Row9 = Range("A39", "AA41")
    
    i = 5
    Rng = Row&i&
    End Sub
    What I want to do is create a range (Rng) depending on the value of i (here simply set to 5 for ease of reading). I know what I have written above is incorrect, but I just don't know how to fix it! Any thoughts much appreciated - thanks in advance.

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: use a variable within a range name

    To get A5 thru A7:


    Sub marine()
    Dim i As Integer, r As Range
    i = 5
    Set r = Range("A" & i & ":A" & i + 2)
    MsgBox r.Address
    End Sub
    Gary's Student

  3. #3
    Registered User
    Join Date
    09-18-2012
    Location
    Banbury, England
    MS-Off Ver
    Excel For Mac 2011
    Posts
    3

    Re: use a variable within a range name

    Thanks, but I'm not sure if that does what I need it to...
    What I want is if i = 5 (for example), then Rng is equal to Row5, and if i=8, then Rng is equal to Row8.

    (by the way, 'Sub marine()' gave me a giggle)

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: use a variable within a range name

    I apologize for the humour.

    In your example, Row5 is the name of a distinct range. To access it from a group of ranges, you need an array of ranges:


    Sub dural()
    Dim roww(1 To 5) As Range, Rng As Range
    Set roww(1) = Range("A15", "AA17")
    Set roww(2) = Range("A18", "AA20")
    Set roww(3) = Range("A21", "AA23")
    Set roww(4) = Range("A24", "AA26")
    Set roww(5) = Range("A27", "AA29")
    
    i = 5
    
    Set Rng = roww(i)
    
    End Sub

  5. #5
    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,199

    Re: use a variable within a range name

    Hi, tom_laight,

    one range or five different ones? This goes for one big range (at least on Windows ):

    Dim rngBig As Range
    Dim lngCounter As Long
    Const clngSTART As Long = 15
    Const clngPARTS As Long = 5
    Const clngLENGTH As Long = 3
    
    For lngCounter = clngSTART To (clngSTART + clngPARTS * clngLENGTH - 1) Step clngLENGTH
      If rngBig Is Nothing Then
        Set rngBig = Range(Cells(clngSTART, "A"), Cells(clngSTART + clngLENGTH - 1, "AA"))
      Else
        Set rngBig = Union(rngBig, Range(Cells(lngCounter, "A"), Cells(lngCounter + clngLENGTH - 1, "AA")))
      End If
    Next lngCounter
    'just for demonstration
    rngBig.Select
    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

  6. #6
    Registered User
    Join Date
    09-18-2012
    Location
    Banbury, England
    MS-Off Ver
    Excel For Mac 2011
    Posts
    3

    Re: use a variable within a range name

    I think that's cracked it! Thanks very much to both of you for your swift and helpful replies - I'll be coming here again

    Tom

  7. #7
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: use a variable within a range name

    @ tom_laight

    Welcome to the forum.

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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