+ Reply to Thread
Results 1 to 12 of 12

Problems to store visible cells into array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Problems to store visible cells into array

    Hello to all,

    Please some help on this.

    I'm trying to store into an array the values of filtered/visible cells in range A2:A10. With the code below
    the array only is loaded with the first 3 values (B,C,D), where should be B, C, D, T, K.

    The filter is applied based on those cells colored in red.

    I'm attaching a sample file for your reference.

    Sub Test()
    
    Range("A2:A10").AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
    
    Arr = Range("A2:A10").SpecialCells(xlCellTypeVisible).Value
    
    End Sub
    Thanks in advance.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Problems to store visible cells into array

    Filtered (Visible) cells can only take contagious cells, which means the array can only take two rows at most, but you can set it to a range.

  3. #3
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Problems to store visible cells into array

    Hi AB33,

    Thanks for answer.

    I've tried setting to range but when I see the content of "rng" in "Locals" windows it's still only taking the first 3 filtered values.

    I've done as below.

    Sub Test()
    Dim rng As Range
    
    Range("A2:A10").AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
    
    Set rng = Range("A2:A10").SpecialCells(xlCellTypeVisible)
    
    End Sub

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Problems to store visible cells into array

    Quote Originally Posted by cgkmal View Post
    I've tried setting to range but when I see the content of "rng" in "Locals" windows it's still only taking the first 3 filtered values.

    I've done as below.

    Sub Test()
    Dim rng As Range
    
    Range("A2:A10").AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
    
    Set rng = Range("A2:A10").SpecialCells(xlCellTypeVisible)
    
    End Sub
    Array can store continuous block of cells.
    That means it is equivalent to
    Arr = Range("A2:A10").SpecialCells(xlCellTypeVisible).Areas(1).Value
    So, if you want all the filtered value in an array, you need to copy the filtered data in somewhere then store in an array.

  5. #5
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Problems to store visible cells into array

    Thank you. Understood.

    One more question.

    How to set an "if condition" to chek if in the range A1:A10 there is no cells colored in red?

    Thanks again.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Problems to store visible cells into array

    I have ameended your code. I have also added my own code.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Problems to store visible cells into array

    Hello AB33 and jindon, thanks for the help. Trying with your code (AB33's code) the range contains still the first 3 values. Only selecting all rows up to the last row visible and copy to a temp range it seems to work as jindon says.:confus.

    AB33,

    What does it mean the ampersand after the variable "N" in your declaration (N&)?
    Last edited by cgkmal; 08-11-2014 at 06:59 PM.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Problems to store visible cells into array

    Quote Originally Posted by cgkmal View Post
    Only selecting all rows up to the last row visible and copy to a temp range it seems to work as jindon says.:confus.
    You need to understand that the array can only take the value from the range when it is continuous.

    When the range includes blank row(s)/column(s), it only take the value from the first block of the cells.
    e.g
    If the range is A1:G10 and the visible cells are A1:G2,A5:G6,A8:G8, the array only take the value from the first block of cells, that is A1:G2.

    This is one of the basic characteristics of the array that you need to understand....
    Last edited by jindon; 08-11-2014 at 07:29 PM.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Problems to store visible cells into array

    deleted duplicate

  10. #10
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Problems to store visible cells into array

    Thanks jindon for the example. Exactly to understaand how to set that condition and some other very good techniques you always use.

    Regards

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Problems to store visible cells into array

    One way
    Sub test()
        Dim myRange As Range, r As Range, x As Range, ff As String
        With Application.FindFormat
            .Clear
            .Interior.Color = vbRed
        End With
        Set myRange = Range("a1:a10")
        Set r = myRange.Find("", searchformat:=True)
        If Not r Is Nothing Then
            ff = r.Address
            Do
                If x Is Nothing Then
                    Set x = r
                Else
                    Set x = Union(x, r)
                End If
                Set r = myRange.Find("*", r, searchformat:=True)
            Loop Until r.Address = ff
        End If
        MsgBox IIf(r Is Nothing, "No cells", x.Address)
    End Sub

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Problems to store visible cells into array

    No problem.

+ 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. Copy Visible cells and paste values only to visible target cells
    By wotadude in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-20-2024, 12:01 AM
  2. Creating array of specialcells(visible) returns only first x cells
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-27-2013, 11:04 AM
  3. Store array formula content in a VBA array
    By cgkmal in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2013, 02:24 PM
  4. How to store strings in a array and paste them in individual cells in MS ACCESS..
    By shobinp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2012, 03:36 AM
  5. [SOLVED] Copy/Pasting Values from visible cells (using autofilter) to visible cells
    By evakotsi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2012, 07:49 AM

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