Dear Excel-Experts,
After searching a lot on this an several other boards i came to ther conclusion to create an own post thread, because i can't find an answer for my specific question.
The problem/aim:
I'm trying to fasten up a macro containing countifs-formulas.
Some time ago i solved an similar problem on sumifs-formulas with an scripting-dictionary and get impressed by the speed. Now i want to realize countifs with dictionarys.
I found several examples on "countif" but only a very few on "countifs" and no one matching to my specific problem.
This formula should be replaced by dictionary:
The following variables are pulled out of an other sub:
WS_SAPimp = [Worksheet]
C_SAPimp__KST = C3 [integer]
C_SAPimp__Jahr = C15 [integer]
C_SAPimp__Status = C17 [integer]
Z_SAPimp__Start = R3 [long]
Z_SAPimp__End= R49557 [long]
Code:
This job will be done for ~50.000 rows+.
It's very fast and in the most rows it is counting correctly, but not in all of them and i don't know why.
In C3 (C_SAPimp__KST) are ids written, in C15 (C_SAPimp__Jahr) years. There are three years 2018-2020 and a lot of ids.
In example in the very first id it's giving me an incorrect solution (206 instead of 3) the following ids of this year (2018) are correct.
The first id in the next year (2019) is giving me also an incorrect solution. In the last year (2020) the solutions are correct.
I'm very new at scripting.dictionarys maybe an expert could show where is my mistake or show me on how to do this job better.
Please don't point to "pivot table" or "power pivot" ... because i want to learn something about the dictionary-methode.
Please excause my english, i hope you can understand my problem. It is a little bit hard to explain as an non native speaker.
Best regards
MK91
After searching a lot on this an several other boards i came to ther conclusion to create an own post thread, because i can't find an answer for my specific question.
The problem/aim:
I'm trying to fasten up a macro containing countifs-formulas.
Some time ago i solved an similar problem on sumifs-formulas with an scripting-dictionary and get impressed by the speed. Now i want to realize countifs with dictionarys.
I found several examples on "countif" but only a very few on "countifs" and no one matching to my specific problem.
This formula should be replaced by dictionary:
VBA Code:
.FormulaR1C1 = "=COUNTIFS(R3C3:R49557C3,RC3,R3C15:R49557C15,RC15)"
The following variables are pulled out of an other sub:
WS_SAPimp = [Worksheet]
C_SAPimp__KST = C3 [integer]
C_SAPimp__Jahr = C15 [integer]
C_SAPimp__Status = C17 [integer]
Z_SAPimp__Start = R3 [long]
Z_SAPimp__End= R49557 [long]
Code:
VBA Code:
Dim dic As Object
Dim vARR_SAPimp As Variant
Dim r as long
With WS_SAPimp
Set dic = CreateObject("scripting.Dictionary")
For r= 1 To UBound(vARR_SAPimp)
With dic
.Item(vARR_SAPimp(r, C_SAPimp__KST) + vARR_SAPimp(r, C_SAPimp__Jahr)) = .Item(vARR_SAPimp(r, C_SAPimp__KST) + vARR_SAPimp(r, C_SAPimp__Jahr)) +1
End With
Next r
For r = 1 To UBound(vARR_SAPimp)
With dic
vARR_SAPimp(r,1) = .Item(vARR_SAPimp(r, C_SAPimp__KST) + vARR_SAPimp(r, C_SAPimp__Jahr))
End With
Next r
.Range(.cells(Z_SAPimp__Start, C_SAPimp__Status), .Cells(Z_SAPimp__End, C_SAPimp__Status)) = vARR_SAPimp
End With
This job will be done for ~50.000 rows+.
It's very fast and in the most rows it is counting correctly, but not in all of them and i don't know why.
In C3 (C_SAPimp__KST) are ids written, in C15 (C_SAPimp__Jahr) years. There are three years 2018-2020 and a lot of ids.
In example in the very first id it's giving me an incorrect solution (206 instead of 3) the following ids of this year (2018) are correct.
The first id in the next year (2019) is giving me also an incorrect solution. In the last year (2020) the solutions are correct.
I'm very new at scripting.dictionarys maybe an expert could show where is my mistake or show me on how to do this job better.
Please don't point to "pivot table" or "power pivot" ... because i want to learn something about the dictionary-methode.
Please excause my english, i hope you can understand my problem. It is a little bit hard to explain as an non native speaker.
Best regards
MK91