Results 1 to 2 of 2

Thread: Indexing Named Ranges in VBA

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    33

    Indexing Named Ranges in VBA

    Is there a way to index a named range like an array? For example, in a spreadsheet I name cells F1 to F5 as "DaRange" and in VBA I want to set the second element (which is F2) to 5. Is there a way to do the following?

    Range("DaRange")[2] = 5

    Thanks

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Indexing Named Ranges in VBA

    With "F1:F5" has been named as "DaRange", you can use:

    Range("DaRange")(2) = 5

    Index number 2 refers to the second cell in the range.

    An example in general case: Cell index numbers will be counted from left-to-right and top-to-bottom.
    Code:
        Dim MyRange As Range, i As Long
        
        Set MyRange = Range("C3:E10")
        
        Debug.Print MyRange.Cells(2, 2).Address '-- $D$4
        Debug.Print MyRange(5).Address          '-- $D$4
    
        For i = 1 To MyRange.Cells.Count
            Debug.Print "MyRange(" & i & ") : " & MyRange(i).Address
        Next
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width