• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Faster Calculation for Macros on Ranking?

Dahlia

Member
Hye,

I have been trying to embed this ranking formulas (inspired by Chandoo's contribution on Top X Chart) into pivot table with the intention to let it calculate automatically when the range and data changes. Unfortunately, I really couldn't except the "Dummy" column. So, I converted the formulas as macros. They work successfully, just that consumes inconsiderable amount of time to process even just for 20 to 30 rows of data.

Appreciate is anyone who is expert to correct or improve my coding below to allow much faster processing :-

Code:
Sub Rank()
Dim FPart1 As String
Dim FPart2 As String
Dim FPart3 As String
Dim LRow As Integer
Dim SRow As Integer
Application.ScreenUpdating = False
'LRow = Range("B1", Range("B1").End(xlDown)).Rows.Count
'With ActiveSheet
'LRow = .Cells(.Rows.Count, "B").End(xlUp).Row
'End With
OldRow = Range("E" & Rows.Count).End(xlUp).Row
'MsgBox OldRow
LRow = Range("B" & Rows.Count).End(xlUp).Row
SRow = Range("A2").Value
'MsgBox LRow

ActiveSheet.Range("E" & SRow & ":E" & OldRow).ClearContents

FPart1 = "=IF(XXXXX<=R2C6,INDEX(INDIRECT(""$B$""&R2C1&"":$B$""&LOOKUP(2,1/(C[-3]""""),ROW(C[-3]))),MATCH(LARGE(YYYYY,ROWS(INDIRECT(CHAR(COLUMN()+64)&""$""&R2C1&"":""&CHAR(COLUMN()+64)&ROW()))),YYYYY,0)),IF(XXXXX<=R2C6+1,""All Other"",""""))"
FPart2 = "ROWS(INDIRECT(CHAR(COLUMN()+64)&""$""&R2C1&"":""&CHAR(COLUMN()+64)&ROW()))"
FPart3 = "INDIRECT(""$D$""&R2C1&"":$D$""&LOOKUP(2,1/(C[-3]""""),ROW(C[-3])))"

Application.ReferenceStyle = xlR1C1

With ActiveSheet.Range("E" & SRow)
.Formula = FPart1
.Replace "XXXXX", FPart2, lookat:=xlPart
.Replace "YYYYY", FPart3, lookat:=xlPart
End With

Application.ReferenceStyle = xlA1
Range("E" & SRow).Select
Selection.AutoFill Destination:=Range("E" & SRow & ":E" & LRow)
Calculate
Range("E" & SRow & ":E" & LRow).Select
Range("E" & SRow).Select
Application.ScreenUpdating = True
End Sub

I just realized that the Dummy auto calculation I did in pivot will not give correct calculation when I select "All" for parameter months. :( I guess that is another problem I need to ask the expertise to help. Because I wish to do a validation list for the month names, which is when I select, all the pivots will be filtered automatically and all the charts by all types of data group will be populated automatically. This way I can efficiently complete my stats report either weekly, monthly, quarterly or all.

Attached is the sample file. Appreciate any help to make it better.


Post Moved By Mod.


.
 

Attachments

  • Sample.xlsb
    90 KB · Views: 6
How long does it take for you to run the code?

I tested on my machine (Win 7 64bit, i5-4200M CPU @ 2.50GHz, 16GB Ram).

Excel 2010 (32 bit) - Avg 3.2 sec
Excel 2016 (32 bit) - Avg 2.8 sec

Not too bad of time.
 
Well, around 25secs for just 1month data at the pivot which the source of data consists of 4536 rows of data at the moment and will increase based on month (max 10k) as this should be for one year report. FYI, I developed and run it on VMware because am not familiar doing vba in Excel Mac. Below are the specs on my Macbook Pro and VMWare:-

Model Name:MacBook Pro
Processor Name:Intel Core i5
Processor Speed:2.7 GHz
Number of Processors:1
Total Number of Cores:2
L2 Cache (per Core):256 KB
L3 Cache:3 MB
Memory:8 GB
Boot ROM Version:MBP121.0167.B17
SMC Version (system):2.28f7

VMWare : Version 8.1.1 (3771013)
Excel 2013 Pro Plus (32-bit)

By the way, I added in the coding to copy+paste as values after full results are processed but seems it doesn't improve the processing time either. :'(

I need to have better solutions as I'm asked to do a demo to one of prospect client very soon. I couldn't be having the audience to wait for 25secs for such result that they could get even faster when doing manually. :'( That is not convincing at all.

Please help..anyone?
 
Hmm, check VMWare setting, how much memory did you allocate to virtual machine?

For Excel, is multi-threaded calculation turned on?

What is the exact process for your workbook? From what I see, Refresh button is just updating the formula?

Is what you are trying to accomplish as following?
1. Rank Post Column contain Error Code of Top 3 by Count of CTT#. + Other.
2. Rank Val. Is it just the Count of CTT#?

Would it make sense to rather than use formula, use PivotTable grouping to summarize your data?

See image below for example.
upload_2016-9-13_9-47-27.png
 
Hmm, check VMWare setting, how much memory did you allocate to virtual machine?

For Excel, is multi-threaded calculation turned on?

What is the exact process for your workbook? From what I see, Refresh button is just updating the formula?

Is what you are trying to accomplish as following?
1. Rank Post Column contain Error Code of Top 3 by Count of CTT#. + Other.
2. Rank Val. Is it just the Count of CTT#?

Would it make sense to rather than use formula, use PivotTable grouping to summarize your data?

See image below for example.
View attachment 34462

Hye Chihiro,
I appreciate your help and suggestions. You are correct on understanding of what am trying to accomplish but I did not use pivotable grouping to do it because I want the chart to be able to show those count of CTT# outside the ranking accumulatively as "All Other", including if there is "-" in ErrorCode column. This would then justify to audience on the total shown in the chart (regardless the ranking) will always be tally as what shown at Grand Total field in the table. Also that total for "-" is not counted in the ranking, else it will always show 1st at the Top. This cannot be done by the default pivot table features.

I have deleted the command line "Application.EnableEvents". Somehow after I save the workbook and reopened and retry, it has working fast enough as expected. So, I guess I just drop this question because I have another issue on looping the worksheets instead. I should post in another new post then.

Thanks again for your effort to help.

DZ
 
Back
Top