Consulting

Results 1 to 8 of 8

Thread: Copying Filtered Items to an Array

  1. #1

    Question Copying Filtered Items to an Array

    Hi,

    I am having a problem with adding the items i have filtered to an array.

    Problem: I have applied multiple filters to a spreadsheet to meet my requirements. I have a certain drop down list that i want it to be copied into an array after all the filtering is done.



    I want all the items of this filtered list into an array.

    Hope its not confusing. Thanks for your help.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Array = Range.SpecialCells Type:=xlCellTypeVisible
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    @SamT

    That won't do the whole trick: only the first area will be written into the array.
    You can copy the filtered range to a certain cell. Now it's a range withou empty rows/columns. You can put that range directly into a listbox or combobox using
    [vba]
    Listbox1.list=sheets91).cells(1,100).currentregion.value
    [/vba]

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Since C1 is the Filter column heading, I start at C2. Filter only works down to the first empty row which I guess is what you want.

    [VBA]Sub ken()
    Dim r As Range
    Set r = Range("C2", Range("C2").End(xlDown)).SpecialCells(xlCellTypeVisible)
    MsgBox r.Address

    Dim a() As Variant
    a() = r
    MsgBox Join(WorksheetFunction.Transpose(a), vbLf)
    End Sub
    [/VBA]

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    only the first area will be written into the array
    Thanks, didn't realize that Range.SpecialCells Type:=xlCellTypeVisible would would not return a contiguous range of only the visible cells.

    Or is there something else going on?

    Actually I was thinking of

    [VBA]Set RngA = Range().SpecialCells Type:=xlCellTypeVisible
    Array = RngA[/VBA]

    But I didn't say that, did I?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    @Sam
    Just try:

    [vba]Sub M_snb()
    with sheets(1).cells(1).currentregion
    .autofilter 1, "sam"
    sn=.offset(1).specialcells(12).value
    combobox1.list=.offset(1).specialcells(12).value
    .offset(1).copy sheets(1).cells(1,40)
    .autofilter
    end with
    Combobox2.list=sheets(1).cells(1,40).currentregion.value

    msgbox combobox1.listcount & vblf & combobox2.listcount & vblf & ubound(sn)
    End Sub[/vba]
    Attached Files Attached Files
    Last edited by snb; 05-30-2013 at 07:34 AM.

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thanks.

    That was unexpected.

    And counter intuitive, at that.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You can't return a contiguous range of discontiguous cells (ranges have to exist on a sheet somewhere)
    Be as you wish to seem

Posting Permissions

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