+ Reply to Thread
Results 1 to 15 of 15

Create an array for filtered and non-contiguous columns

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Create an array for filtered and non-contiguous columns

    Hello everyone
    I have in my file filtered data based on column G

    I need to create an array of the values of these filtered data for two columns (B & G) only

    In other word to store the values of these two filtered columns (visible rows only) not all the values
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Create an array for filtered and non-contiguous columns

    Hi,

    I gave you what you asked for, but also added what you probably need instead:
    a. You asked for a two dimensional array
    b. I think you need a one dimensional data structure which contains the two columns.

    Notice that the array is rather cryptic, because you have to rely on the index numbers to determine whether you have an Item or a Mount (Amount). With the data structure you know right away. Of course, you could declare constants for each of the array indices that indicate 'Item' or 'Mount.

    The difference is (simplified non-working examples)
    Please Login or Register  to view this content.
    Actual working code in an ordinary code module such as Module1:
    Please Login or Register  to view this content.
    Lewis
    Attached Files Attached Files
    Last edited by LJMetzger; 07-28-2015 at 08:17 AM. Reason: Replaced download file due to typos that caused compile errors. Posted code was correct. Sorry. Lewis

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    Thanks a lot mr. Lewis for this perfect way ..
    It is excellent but I didn't imagine it would be so long way.
    The two ways are wonderful ..

    Is there an easy way to create UDF function that deal with arrays and we as beginner users use the UDF to have the results quickly?
    I imagine this : CreateFilteredArray(WholeRangeOfData,Column B,Column G)
    =CreateFilteredArray(Range("A4:G92"),2,7)

    Thanks for your great and awesome help

  4. #4
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Create an array for filtered and non-contiguous columns

    Try this out:

    CopyFiltered.xlsm

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    @ cyiangou
    Amazing UDF fuction. I consider it very useful too that can save time and effort
    Thanks a lot for this great and wonderful gift

    Just a little modification if possible ... the first parameter to be more spefici that's :
    Worksheets("Sheet1").range("A3:G3")

    Please Login or Register  to view this content.
    Range("A3:G3") is the title header of the filtered range
    Thanks alot for this great help
    Last edited by YasserKhalil; 07-26-2015 at 02:03 PM.

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    I noticed that there is a reference "Microsoft Forms 2.0 Object Library" and I tried to check it to activate it in my file but I didn't find the reference.
    I'm using office 2013

  7. #7
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Create an array for filtered and non-contiguous columns

    The UDF is using a helpful feature of the autofilter itself (.AutoFilter.Range.Copy) that lets us exclude the hidden rows, so it has to be a reference to an actual autofilter definition, else we can't use this. But you can only have one autofilter per sheet (I think), so isn't this where you'll want to point to anyway?

    If the code works without the reference, then you don't need it. Probably built in to 2013.

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    It works well with your attachment
    After copying the code to my file ,I got error "user-defined type not defined" Compile error
    Any idea about that

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    I clicked Browse to add the reference and browse in system32 folder for FM20.DLL and add it
    I get rid of the error "User-Defined type not defined"

    After that I tested the code again and I got the following error
    Please Login or Register  to view this content.
    Object variable or with block variable not set

    Any idea

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    Thanks .. I could fix it ..
    I just need a little modification >> Just to add a line that checks auto filter mode .. if it is true then the code to be executed but if it is false to exit sub

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Create an array for filtered and non-contiguous columns

    Thanks everyone
    I have added this line to solve the problem
    Please Login or Register  to view this content.
    Thanks a lot for any help me even if with a simple idea

  12. #12
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Create an array for filtered and non-contiguous columns

    Thanks for the rep points YasserKhalil.

    YasserKhalil wrote:
    Is there an easy way to create UDF function that deal with arrays and we as beginner users use the UDF to have the results quickly? I imagine this : CreateFilteredArray(WholeRangeOfData,Column B,Column G)
    =CreateFilteredArray(Range("A4:G92"),2,7)

    The function you want can not be used in a formula (see the next post for reasons):
    Please Login or Register  to view this content.

    Try the following function (a slight modification of function CreateStructureArrayOfFilteredData() in post #2 above, which can be used in other VBA code. All the code that follows can be tested using the data from the file in post #2 above.
    Please Login or Register  to view this content.
    The following are a few ways to test the above function.
    Please Login or Register  to view this content.
    Lewis
    Last edited by LJMetzger; 07-28-2015 at 11:45 AM.

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Create an array for filtered and non-contiguous columns

    Just FYI, you can late bind that function from cyiangou so you don't need the MS Forms reference each time:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  14. #14
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Create an array for filtered and non-contiguous columns

    YasserKhalil wrote: ... beginner users use the UDF ...
    This post is only peripherally related to the thread.

    UDFs or User Defined Functions:
    Reference: http://www.ozgrid.com/VBA/Functions.htm#UDFIntro
    Simple Tutorial Reference: http://excelvbatutor.com/index.php/excel-vba-lesson-11/

    UDFs are a subset of VBA functions (and Subs).

    Some UDF Rules:
    a. Must be in an Ordinary Code Module (e.g. Module1) and can't be in a Sheet Module.
    b. Can't change the structure of a SpreadSheet.
    c. Can't change the physical characteristics of a cell (font, color).
    d. Can only implicitly (using the return value) change the value of the cell in which the UDF resides.
    e. Can be called from VBA.

    Some simple UDF examples:
    Please Login or Register  to view this content.
    VBA code to test the above UDFs:
    Please Login or Register  to view this content.
    Lewis

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Create an array for filtered and non-contiguous columns

    Just for completeness, it is possible to work around b, c and d and enable a UDF to do pretty much anything a Sub can do, but it's complicated (either requiring Windows timers or using event code as well) and not usually a good idea.

+ 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 contiguous columns and paste as non-contiguous
    By absconditus in forum Excel General
    Replies: 1
    Last Post: 12-29-2014, 03:17 AM
  2. Array formula to create list of filtered results
    By Kris_cs1 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-16-2014, 02:34 AM
  3. Using non-contiguous cell references in an array
    By phook01 in forum Excel Formulas & Functions
    Replies: 22
    Last Post: 03-26-2014, 03:10 PM
  4. [SOLVED] How to sum non-contiguous columns applied as a formula on contiguous cells
    By figo12 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 10-09-2013, 01:07 PM
  5. Populate 2D-array with non-contiguous range
    By Laksefar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-11-2013, 06:37 AM
  6. Copy and Paste an array (contiguous & non contiguous ranges)
    By Xrull in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2010, 09:17 AM
  7. [SOLVED] Copying non-contiguous columns to contiguous columns
    By Bob in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2006, 10:54 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