+ Reply to Thread
Results 1 to 15 of 15

VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

  1. #1
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hi,

    So the following questions are for clarity, as I'm trying to learn VBA to build some tools but need clarity on some syntax/rules. If you have the answers or can point me to clear and unambiguous resources, I will be extremely grateful

    1. Filling/Printing the Array: What are the exact rules for Filling with a Loop vs. filling with Range

    i.e. Array(9, 9) = .Range("A1:A10") vs. 2 Nested FOr Next loops as an example.

    There seems to be some constraints against manipulating the data loaded as a range but I can't get a for sure answer.


    2. Selecting Data from an Based on Criteria:
    I don't yet have full grasp on arrays, and I'm trying to learn (no prior programming experience), so while I understand collections can provide some flexibility, I would like to understand Arrays a little better before moving to the next topic.


    That said, I am using a combination of Named ranges/Arrayformulas/my poor attempts at VBA. Ultimately I'm trying to learn how to load a sheet of data into an array and then select data based on Criteria.
    i.e. array(0 to 200, 0 to 150) = Range("A1:ET200") or = [BlueBell] and then I'd like to select everything from Column 2 that meets some criteria *without using the autofilter method. The way I am considering now involves selecting that loading an array operation in a wksheet formula into an array and then finding a way to remove blanks or FALSE.

    I have attached a workbook, of a tool I'm tinkering around with to have reference data if this helps


    Thanks for any help.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Please Login or Register  to view this content.
    http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Sorry if I was unclear, but this doesn't address any of my questions really. For instance I know you can copy and paste arrays of data by Looping or by pasting the whole range. What I'm trying to understand are the limitations of taking one approach vs. the other relative to manipulating data in an array in general. So if I load with a loop what can/cannot do if I load with a range what I can/cannot do.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hello cmore,

    If you need to apply or check formats, or access properties or methods of a Range then use a loop.

    If you only need to copy the values of the range then use an array.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Thanks Leith. So ultimately, if I create an array through a range, then I can no longer do anything to the individual elements? If so, does this also apply to an array operation housed in a named range? so if I have a formula that evaluates to ("", A, B, 1, 3, "", D)?

    Thanks for any clarification on both sets of questions.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hello cmore,

    The restriction of using the array is you can only read or write to the Range cells. You can not change the cell properties, methods, or formatting.

    All Range cells are Variant typed by default. The array also needs to be a Variant type. What ever the cell contains, the same cell in the array will be of the same Type.
    Please Login or Register  to view this content.
    The variable arr will be automatically dimensioned to match the Range. Ranges are always 2-Dimensional and 1 Based. That is the subscripts always start with one.

  7. #7
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    THANK YOU!! This has clarified so much of what I've been reading and mucking up. So if I evaluate the formula in a Named range, this won't technically be a Range object. I guess my second question then would be, how do I and is it possible to load specific elements into a new array based on criteria?

    I've seen autofilter as the primary option, but seems inflexible given the header requirement. If I have array(0 to 9, 0 to 9), can I pull out items within Column 2 that meet certain criteria? The way I'm thinking now is, use a formula in a named range, get values then I just need a way to get rid of blanks/falses. But I'm not sure if there a better way of doing that, assuming I haven't moved in the world of collections

    Data attached
    Attached Files Attached Files
    Last edited by cmore; 09-15-2013 at 06:43 PM. Reason: add attachment

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hello cmore,

    There are 3 key Excel functions that you can use on your arrays: Transpose, Index, Match. There are a few more, but this are the most flexible.

    Rather than use the WorksheetFunction class, I generally use the Application class. The Application class calls will not stop VBA if an error occurs. It is important to check the returned valued to make sure it is not an error before you proceed. This is easily done with Variants by using the VBA IsError(<variant>) method or by checking the Variant Type of the returned value using VarType(<variant>). If it is 10 then it is an error.

    TRANSPOSE will "flip" an array. The columns become rows and rows become the columns. This is all done internally with no looping code needed.

    INDEX allows you to return an entire row or entire column from an array. The down side is you can not use it to write a row or column of data. You have to use a loop.

    MATCH allows you match an element in array and use the wild cards * and ?. Case is ignored. The relative position of the matched cell in the array is returned.

    Please Login or Register  to view this content.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hello cmore,

    This post addresses the sorting issue. I created this routine a little over 3 years ago. It uses a bubble sort and the Text Compare function to sort the data. This macro is very fast because rather than move the data during the sort, it moves the variable pointers to the data. You will need to select the proper API call for your version of Windows. Only one call can remain in the macro.

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Leith,

    Thanks a lot, this is the most help I have ever received, including the $100 I spent on a couple books. I think I have what I need to try and put some things together a little better now, given what you've mentioned. I have re-attached a work book with generally what I am trying to do. On the Contents Tab, Column K vs. Column L. There's no criteria selection, but it's basically the same kind of operation. I am going to spend some time wrapping my head around your version this Bubble, figuring out how I can use this Application.Index Application.Match to optimize Please let me know if you have any other thoughts.
    Attached Files Attached Files

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hello cmore,

    Thanks for posting the new workbook. This is quite large. It will take me some time to digest what you are doing and what you want to improve.

  12. #12
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Quote Originally Posted by Leith Ross View Post
    MATCH allows you match an element in array and use the wild cards * and ?. Case is ignored. The relative position of the matched cell in the array is returned.
    Hi Leith Ross, can you post an array match example ?

  13. #13
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Quote Originally Posted by Leith Ross View Post
    Hello cmore,

    Thanks for posting the new workbook. This is quite large. It will take me some time to digest what you are doing and what you want to improve.
    Sorry for the confusion, so ultimately, want to build a tool that reports testing data and the key driver seems to be extracting data and summarizing it, charting it, or using it in a some kind of activex-control to drive further analysis. Initially was using array formulas to do this, but at 5000+ rows it became a problem. So now I'm trying to find a smart way to reduce the named ranges and develop method to extract data and ultimately learn VBA/coding really.

    So I was really wondering, if you look on the contents tab. I have an array worksheet formula vs. a macro tied to a controlbutton.

    The control is loaded with named range that holds a formula operation that is then placed into an array with all non-blank strings. Based on what you're saying I should be able to use VB to cut the data against criteria, without naming the range? and/or Are there a other approaches I should consider?

    Please Login or Register  to view this content.
    Last edited by cmore; 09-16-2013 at 09:04 PM.

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Hello cmore,

    You really have fallen in love with named ranges. So much so, I really can not follow the logic of what you are trying to do here. There are just too many levels of indirection for me to follow and I don't have the time to map them. If you can talk me through the process then I can offer you some guidance.

  15. #15
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: VBA Arrays: Loading with Range vs Loop and selecting data given Criteria

    Yeah they've gotten the best of me.

    So ignore what I've done thus far, as one of my hopes is to minimize the named ranges and get things in arrays
    Static (generally) Tabs: Answers, Typology, Conversion

    Summary: sets the the test info to populate on the Data tab i.e. Test 3 has x number of questions will generate that many rows in Data Tab and all the associated info from Answers and Typology this is based on columns BC to BO.

    Summary: also is aggregate of performance in Data

    Data: Is User answers and then associated information from Answers and Typology

    So what I want to do involves a lot but first step is find efficient way to load data and select data based on criteria.
    1. To show answers to user on granular level 'Data tab
    2. To Show aggregate of that 'Summary
    3. To fill Listbox Controls/charts

    all on a conditional basis
    As a test I wanted to see if I can l can get the Tests not taken and load them into an array similar to what I did formulaically (see contents! J2)

    relevant arrays:
    LSACReleased - dynamic range AJ2:AJ75 = All Tests available

    PTestsTaken - looks at Named Range MyLSAC(SummaryC7:C75) and compares to All TestsAvailable), Match then select if not then ""
    PTestsNOTTaken - looks at PTestsTaken and then any "" will select that show that test from Tests Available

    From the VBA code, I basically evaluate PTestsNOTTaken into the array PTesteroni() for all non-null strings ""

+ 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. Dynamic Arrays - Selecting some rows based on criteria
    By AHFoddeR in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-23-2013, 03:55 AM
  2. Selecting a range using a loop
    By slash_gnr3k in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2012, 06:38 PM
  3. Help with Loop + Selecting data from Range
    By rsim in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-03-2012, 02:45 PM
  4. Loading Arrays in VB
    By Marston in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-26-2011, 08:16 PM
  5. Loading Arrays in VBA
    By mattflow in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-30-2010, 02:33 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