+ Reply to Thread
Results 1 to 5 of 5

Query rows from a named range in active sheet as an array

Hybrid View

  1. #1
    Registered User
    Join Date
    05-30-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    16

    Query rows from a named range in active sheet as an array

    I am trying to take a named range in my sheet and refer to it in VBA in order to query its rows to use in Array(Extracted Rows Here). Thanks so much in advance for the help if anyone thinks of a way.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Query rows from a named range in active sheet as an array

    Hi chickpeazy,

    Not sure if this is exactly what you need but it is a starting point... it will load an array with the row numbers of the named range, just change the sheet name, and the name of the named range.

    Sub chickpeazy()
    
    Dim myRow As Range
    Dim myArray() As Integer
    Dim i As Long
    
    For Each myRow In Worksheets("Sheet1").Range("NamedRange").Rows
        ReDim Preserve myArray(i)
        myArray(i) = myRow.Row
        i = i + 1
    Next myRow
    
    End Sub
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    05-30-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    16

    Re: Query rows from a named range in active sheet as an array

    Thank you for that! I think you understand what I am trying to do, but implementing it in code that another user here helped make does not do anything for some reason.
    Also, thinking about this... an easier way to accomplish what I am trying to do might be to change the color of all cells in the named range because the named range just consists of rows anyway?

    PHP Code: 
    Option Explicit
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        
        
    Dim varMyRows As Variant
        Dim varMyRow  
    As Variant
        Dim colFilNorm 
    As Long
        Dim colHighlight 
    As Long
        Dim rowsForArray 
    As Long
        Dim myRow 
    As Range
        Dim myArray
    () As Integer
        Dim i 
    As Long

        
    For Each myRow In Worksheets("Sheet1").Range("corValTest").Rows
            ReDim Preserve myArray
    (i)
            
    myArray(i) = myRow.Row
            i 
    1
        Next myRow
        
        colFilNorm 
    ActiveCell.Interior.Color
        colHighlight 
    RGB(2552550)
        
        
    varMyRows = Array(myArray'<- Row(s) to be coloured upon cell B2 being activated
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        For Each varMyRow In varMyRows
            Rows(varMyRow).Interior.Color = ActiveCell.Interior.Color
        Next varMyRow
        
        If Target.Address = "$B$2" Then
            For Each varMyRow In varMyRows
                Rows(varMyRow).Interior.Color = colHighlight
            Next varMyRow
        End If
        
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With

    End Sub 
    Last edited by chickpeazy; 05-31-2017 at 11:54 AM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Query rows from a named range in active sheet as an array

    Why do you have this, myArray is already an array?
      varMyRows = Array(myArray) '<- Row(s) to be coloured upon cell B2 being activated
    Also, if you wanted to format the range you wouldn't need an array or a loop.
    Worksheets("Sheet1").Range("corValTest").Interior.Color = ActiveCell.Interior.Color
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    05-30-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    16

    Re: Query rows from a named range in active sheet as an array

    Ah, yes that command seems to be much more succinct... thank you! Though this code still doesnt do what I want (to highlight the named range cells when "B2" is selected and unhighlight otherwise)

    Option Explicit
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        
        Dim colFilNorm As Long
        Dim colHighlight As Long
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
          
        colFilNorm = ActiveCell.Interior.Color
        colHighlight = RGB(255, 255, 0)
        
        Worksheets("Sheet1").Range("corValTest").Interior.Color = colFilNorm
        
        If Target.Address = "$B$2" Then
        Worksheets("Sheet1").Range("corValTest").Interior.Color = colHighlight
        End If
        
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
    End Sub
    Last edited by chickpeazy; 05-31-2017 at 03:26 PM.

+ 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. Sheet codenames work in array but not named range
    By StevePM in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 04-12-2016, 08:43 AM
  2. [SOLVED] Calculate the Active Named Range
    By Joy C in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-17-2014, 12:10 PM
  3. Replies: 1
    Last Post: 11-28-2013, 05:38 AM
  4. Array formulas referencing a named range or named table
    By anrichards22 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-03-2013, 11:59 PM
  5. Find max value in named range and put this + 1 in active cell
    By mlun in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-02-2012, 11:55 AM
  6. Select Named Range - Active sheet - Message Box address
    By al007 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2005, 03:40 AM
  7. Is the active cell part of a named range
    By Virginia in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-23-2005, 02:07 PM

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