+ Reply to Thread
Results 1 to 13 of 13

Help with comparing Variant Array with itself

  1. #1
    Registered User
    Join Date
    06-02-2014
    Posts
    11

    Help with comparing Variant Array with itself

    Hi. I am trying to get this code to work but when I run it to str1, I get Subscript out of range. I need this code to run very efficiently so I used a variant instead of looping through all cells. I have ~ 300k rows and this same operation was difficult for me to do in access.

    The code essentially saves a column as variant, and calculates a proportion with 1 / (Number of times that string matches all others) in the next column's cell over. Hope you can see what I am trying to do. Also please disregard any error "cant divide by zero". Thanks in advance!


    Please Login or Register  to view this content.
    Last edited by mtpyankee; 07-18-2014 at 12:47 PM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Help with comparing Variant Array with itself

    I think you are using the join function incorrectly. It looks like you are using it like the CONCATENATE() function where you are expecting it to join two strings. According the help file (http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx ), the Join function takes a one dimensional array and joins each element of the array, divided by the assigned delimiter. In observing the "compare" variable in the Locals/Watch window, it appears that VBA is treating compare as a 2D array (even though one dimension is 1 to 1), and not accepting compare as an argument for the join function. I'm not exactly sure what you are doing with the join function (I don't see why you are trying to join the array into a single long text string). If you want to use the join function, you are going to need to do something different in assigning the array to compare.

    One observation. It seems like all this is doing is a relatively simple COUNTIF(). U2=1/COUNTIF($A$1:$A$300000,A1) (or whatever the reference is). Depending on where this task fits into the overall project, it might be easier to use a worksheet function rather than VBA.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    06-02-2014
    Posts
    11

    Re: Help with comparing Variant Array with itself

    Thanks for the reply!

    Yes, I am most likely using the join function incorrectly for that was not my intention. I know you cannot compare two elements of variant arrays using = , eg. compare(i) = compare(j) so I was trying to get around that.

    I did have code that used a countif function previous to this. However, it took WAY too long to run the macro. Rumor has it, using variants can cut down on the time, so I was trying to play around with those.

    Would you have any advice to get around the compare(i) = compare(j) dilema?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Help with comparing Variant Array with itself

    Why can you not test compare(i)=compare(j) [[or compare(i,1)=compare(j,1) as 2D arrays]]? This is not the kind of VBA programming I do, so I'm not very good at it, but I have never heard that you cannot test two variants for "exactly equal to". I have heard of other issues with testing for "exactly equal to" (round off error and such), but not that variants cannot inherently be so tested. It would appear to me that, with the recognition that compare is actually a 2D array, it should be fairly straightforward to test for compare(j,1)=compare(i,1).

    Using variants can be one trick to speed some of these things up sometimes. In some ways, though, the hard reality of what you are doing (as I understand it so far) is that your current algorithm is asking the computer (whether using VBA or using the COUNTIF() worksheet function) to perform 90 trillion (300000*300000) operations, and that is just to test compare(i)=compare(j). That does not include incrementing loop counters, retrieving data from the spreadsheet or putting data in the spreadsheet and so on.

    I suspect that, if VBA is really going to be useful for speeding this operation up, you will need a better algorithm -- not some "trick" based on variable data type. For example: could you sort the input data? If the input data were sorted, you would not need to test every entry. You would only need to count the number "A"s in a localized block until you come to an "AB" entry, then you can count the AB entries.

    As I noted, this is not the kind of programming I do, so there are certainly others here may be better at developing this type of algorithm. I think you will need to research some other ideas to really see any gains in computation time.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help with comparing Variant Array with itself

    Can you describe the contents of the data?

    Ae you just trying to generate a histogram of the data?
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    06-02-2014
    Posts
    11

    Re: Help with comparing Variant Array with itself

    Quote Originally Posted by shg View Post
    Can you describe the contents of the data?

    Ae you just trying to generate a histogram of the data?
    A generic example would look as follows: no fields are actually these.

    HTML Code: 
    So when I do pivot tables, if I wanted by country and terrain, I can sum the proportion as my values to avoid double counting.

    Hope this makes sense!

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help with comparing Variant Array with itself

    So the result in the Proportion column is the goal of the calculation?

    How is it calculated? Just the reciprocal of the count of Name?
    Last edited by shg; 07-18-2014 at 02:47 PM.

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Help with comparing Variant Array with itself

    Is this a VBA version of COUNTIF?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Registered User
    Join Date
    06-02-2014
    Posts
    11

    Re: Help with comparing Variant Array with itself

    Calculation is done by something like: 1 / Countif($F$2:$F$6,F2) for each cell in column G if you were to use my example.

    However, when I run that, or even use a WorksheetFunction.Countif() statement, it takes SO long and I dont know if it is even working after a point. I am looking for a way to optimize the process whether using arrays or whatever if possible.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help with comparing Variant Array with itself

    If you sort by name, the calculation could be done much faster by limiting the range reference to a small fraction of the columns. Or you could read the name column into an array and use a dictionary.

    BTW, there is no issue with comparing variants to one another.

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Help with comparing Variant Array with itself

    How many unique entries are there in column A? One thing I observe is that G3 is repeating the exact same calculation done in G2. If there were enough duplication like this, some form of "I've already counted 'Bob', so i don't need to do it again." Could improve computation time. Maybe build a second list with "unique" names from column A, then use the COUNTIF() or even FREQUENCY() functions.

    Or, as I suggested earlier, if the data were sorted on "name" so that you could be certain that all of Bob's entries will be together in the list, then you should be able to limit your count range to the immediate area around Bob's entries, rather than needing to count across the entire 3E5 rows in the database.
    Last edited by MrShorty; 07-20-2014 at 09:52 PM.

  12. #12
    Registered User
    Join Date
    06-02-2014
    Posts
    11

    Re: Help with comparing Variant Array with itself

    Thanks to you both... comparing the variants did work, I probably had an issue somewhere else in the code when I tested it first.

    I will try the sorting and see how it comes out

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help with comparing Variant Array with itself

    Right. The companion part after sorting is using a COUNTIF formula starting in the first row that only looks down far enough to catch the max number of duplicates.

+ 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. Variant vs Array
    By Kyle123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-26-2012, 09:45 AM
  2. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2005, 01:54 AM
  3. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 12:30 AM
  4. [SOLVED] Moving a Variant to Array
    By Ivan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2005, 08:05 AM
  5. ReDim Object array as parameter of Variant array
    By Peter T in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-10-2005, 10:06 AM

Tags for this Thread

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