+ Reply to Thread
Results 1 to 5 of 5

Code optimization - WorksheetFunction.SumIfs() function

  1. #1
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Exclamation Code optimization - WorksheetFunction.SumIfs() function

    Hi there,

    I have this data I need to work with. In the first worksheet, it's a list of 20k phone numbers with basic account info (owner's name, client id, subdivision id, model of phone, etc.)
    In the second worksheet, I have one row per USOC per phone number. So say the number 1234567890 has a voice data plan, and a data plan, the second worksheet would have 1 row for the voice plan, and one row for the data plan.

    There are about 75k rows in this second worksheet.


    Now, I need to create this worksheet with these columns:
    customer_id, phone_number, carrier, has_voice, has_data, has_callerid, has_voicemail, has_email2sms

    So I created a VBA macro that would grab basic info from worksheet(1) and do some work to find the info from worksheet(2). This is where it gets complicated. Since we're migrating to a new system, we need to migrate to new USOC code (USOC code is simply a unique id for each cellphone plan options). Basically, it works like this:

    If you had any voice plan, I need to set the column 'has_voice' to true. So if number 1234567890 has USOC "RVOICE", "TVOICE" or "BVOICE", set 'has_voice' to true.

    I created a function that does just that and it works well. The thing is that it takes for ever to run through all records. I set a break point to stop every 1000 rows (of the 20k rows) and it takes a few minutes between each break. I'm disabling screen updating, manually update formula, etc.

    There must be a better way that to call the SumIfs function as many time as USOC for every phone number.

    So basically, the peudo-code is like this
    Please Login or Register  to view this content.
    So you can see the function ForfaitVoix() and the underlying QuantityCodesUsocs() function.

    How could I optimize this to run a LOT faster?

    Please Login or Register  to view this content.
    Last edited by nfuids; 10-12-2012 at 09:52 AM.

  2. #2
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,885

    Re: Code optimization - WorksheetFunction.SumIfs() function

    Looping like that is very very slow. Do you need VBA? It would be much faster to put the formulas in the sheet.
    Or you can have VBA put the formulas in the sheet all at once, no loops.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  3. #3
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Code optimization - WorksheetFunction.SumIfs() function

    Humm... the formula would have to be a sum of sumifs. One SumIfs per USOC code.

    =IF((SUMIFS(I:I;C:C;"418717XXXX";G:G;"RDATA") + SUMIFS(I:I;C:C;"418717XXXX";G:G;"BDATA")) > 0, "True", "False")

    So considering we have 20k phone numbers, and each phone numbers has 9 column of plan options, it makes just short of 200k formula like that. it seems to me there should be a better option.

    I guess I could be copying the formula using a R1C1 formula for all plan options cells?

    Any other ideas?

    Regards,
    Mart

  4. #4
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,885

    Re: Code optimization - WorksheetFunction.SumIfs() function

    I tried a basic lookup formula:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    It takes about 1.5 seconds to fill down and calculate for 200 000 rows.
    Then I tried the same thing in VBA and it takes about 16 seconds.
    Please Login or Register  to view this content.
    I would expect a greater difference.
    However, at one point I ran it from the VB editor with a Debug.print and then i took 70 seconds. Apparently small things make a big difference. I used timer to check the time as you can see in my code. Maybe if you put Timer in different sections of your code you will find the bottleneck. Maybe there is an issue with the array.

    I don't know anymore than this, it's out of my range of knowledge.
    Anyone else?

  5. #5
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Code optimization - WorksheetFunction.SumIfs() function

    I did the timer thing and realized that it only takes 0.05 second to run the 10 query to determine true/false for the 10 options. So it looks fast enough.

    But if I consider I have 20k records to process, 20000 * 0.05 = 1000 seconds, which equals almost 17 minutes. So I guess I'm out of luck with this... I guess I would need multi-thread.. I might try to copy the formula directly in excel and see if that can improve stuff a little.

    Any other suggestion would be appreciated!

    Regards,
    Mart

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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