Dictionary For loop with Multiple If and range

kirankoushik

New Member
Joined
Feb 19, 2021
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am trying to create a count for each item in the dictionary, where the values in 2 other columns are > 0

I am only getting the dictionary count as output each time. Please help me understand where I am going wrong.

VBA Code:
    dic.CompareMode = vbTextCompare
    Set rRngVisible = Sheets("ScrewPoints").Range("A2").CurrentRegion
        
    For Each rRow In rRngVisible.Rows
            dic(rRow.Cells(2, 6).Value) = ""
    Next rRow
    
    Sheets("Total").Range("C40").Value = dic.Count
    
    k = 0
    
    For Each itm In dic
        'rRngVisible.AutoFilter Field:=6, Criteria1:="5282 01"
        'If Range(AG3 & AG) > 0 & Range(AH3 & AH) > 0 Then k = k + 1
        'k = Application.CountIfs(Range("AG3:AG" & sLastRow), ">0", Range("AH3:AH" & sLastRow), ">0")
        
       If (Sheets("ScrewPoints").Range("AG3:AH" & sLastRow).Value = 0) And _
          (Sheets("ScrewPoints").Range("AH3:AG" & sLastRow).Value = 0) Then
        k = k + 1
        End If
    Next itm
    
    Sheets("Total").Range("C41").Value = k
    k = 0

Thanks

kiran
 
Since the code uses Sheets("Screwpoints").Range("A2").CurrentRegion, make sure that your sample data is contiguous. If there are "blank" columns in-between data, you need at least a header row to conjoin everything. Otherwise, the code fails with CurrentRegion, as shown with the debug printed address.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Since the code uses Sheets("Screwpoints").Range("A2").CurrentRegion, make sure that your sample data is contiguous. If there are "blank" columns in-between data, you need at least a header row to conjoin everything. Otherwise, the code fails with CurrentRegion, as shown with the debug printed address.

whoa.. it works now!

1620025832736.png



However, one last issue with the count..

it shows 3 and 34... it should be 2 and 35 from the sample data..

1620025940109.png
 
Upvote 0
I am guessing we are looking at different samples.
Mine shows:
$A$1:$M$65
$A$2:$M$4,$A$8:$M$8,$A$10:$M$10,$A$12:$M$12,$A$17:$M$19,$A$22:$M$22,$A$24:$M$24,$A$26:$M$30,$A$35:$M$37,$A$39:$M$42,$A$46:$M$58,$A$65:$M$65

Can you show your current sample, with an added column that indicates which row should be OK/NOK?
 
Upvote 0
I am guessing we are looking at different samples.
Mine shows:
$A$1:$M$65
$A$2:$M$4,$A$8:$M$8,$A$10:$M$10,$A$12:$M$12,$A$17:$M$19,$A$22:$M$22,$A$24:$M$24,$A$26:$M$30,$A$35:$M$37,$A$39:$M$42,$A$46:$M$58,$A$65:$M$65

Can you show your current sample, with an added column that indicates which row should be OK/NOK?

Book1.xlsm
ABCDEFGHIJKLMN
1
2abcdescrewcaseghFinishTotal per setTotal per casePDMKS & DVResult
35282 01Final0.000 NOK
47238 01Final1.001 OK
57206 01Final0.000 NOK
65638 01STG11.001 NOK
75638 01STG11.001 NOK
87919 01STG22.002 NOK
97919 01Final2.002 NOK
107919 01STG22.002 NOK
117919 01Final2.002 NOK
127225 01STG11.001 NOK
137225 01Final1.001 NOK
147919 01STG22.002 NOK
157919 01STG22.002 NOK
167919 01STG22.002 NOK
177919 01STG22.002 NOK
187919 01Final2.002 NOK
197919 01Final2.002 NOK
207245 01Final1.001 NOK
217225 01STG11.001 NOK
227920 01STG22.002 NOK
237920 01Final2.002 NOK
247920 01STG22.002 NOK
257920 01Final2.002 NOK
267225 01STG11.001 NOK
277226 01Final1.001 OK
287204 01Final2.002 NOK
297204 01Final2.002 NOK
307204 01Final2.002 NOK
317204 01Final2.002 NOK
327919 01STG22.002 NOK
337919 01STG22.002 NOK
347919 01STG22.002 NOK
357919 01STG22.002 NOK
367919 01Final2.002 NOK
377919 01Final2.002 NOK
387245 01Final1.001 NOK
397225 01STG11.001 NOK
407204 01Final2.002 NOK
417204 01Final2.002 NOK
427204 01Final2.002 NOK
437204 01Final2.002 NOK
444227 01STG32.002 NOK
454227 01STG32.002 NOK
464235 01STG32.002 NOK
477203 01Final2.002 NOK
487203 01Final2.002 NOK
497203 01Final2.002 NOK
507204 01Final2.002 NOK
517204 01Final2.002 NOK
527204 01Final2.002 NOK
537204 01Final2.002 NOK
547204 01Final2.002 NOK
557204 01Final2.002 NOK
565652 01Final2.002 NOK
575652 01Final2.002 NOK
585651 01Final2.002 NOK
595651 01Final2.002 NOK
604227 01STG32.002 NOK
614235 01STG32.002 NOK
624227 01STG32.002 NOK
634235 01STG32.002 NOK
647225 01STG11.001 NOK
657919 01STG22.002 NOK
667919 01Final2.002 NOK
Screwpoints
 
Upvote 0
Just to test, I deleted 3 NOK rows with finish as Final and ran the macro..

i was expecting 2 and 32 as result.. it gave me 4 and 30..

VBA Code:
Sub Import()

Dim K As Long, KK As Long, K2 As Long, K3 As Long
Dim rRngVisible, rRow As Range

    Set dic = CreateObject("scripting.dictionary")
    dic.CompareMode = vbTextCompare
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        Debug.Print .Address '<--
        .AutoFilter Field:=9, Criteria1:="Final", Operator:=xlFilterValues
        Set rRngVisible = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        Debug.Print rRngVisible.Address '<--
    End With

    For Each rRow In rRngVisible.Rows
        dic(rRow.Cells(1, 6).Value) = Application.Max(dic(rRow.Cells(1, 6).Value), IIf(rRow.Cells(1, "J").Value = 0 And rRow.Cells(1, "K").Value = 0, 0, 1))
        dic(rRow.Cells(1, 6).Value & "~C42") = Application.Max(dic(rRow.Cells(1, 6).Value & "~C42"), IIf(rRow.Cells(1, "J").Value > 0 And rRow.Cells(1, "K").Value > 0, 0, 1))
        dic(rRow.Cells(1, 6).Value & "~C43") = dic(rRow.Cells(1, 6).Value & "~C43") + rRow.Cells(1, "J").Value
        dic(rRow.Cells(1, 6).Value & "~Count") = dic(rRow.Cells(1, 6).Value & "~Count") + 1
        dic(rRow.Cells(1, 6).Value & "~C44") = rRow.Cells(1, "K").Value
    Next rRow

    Sheets("Total").Range("C40").Value = dic.Count / 5

    K = 0
    KK = 0
    K3 = 0
    k4 = 0

    For Each itm In dic
        If Right(itm, 4) = "~C42" Then
            KK = KK + IIf(dic(itm) = 0, 1, 0)
        ElseIf Right(itm, 4) = "~C44" Then
            k4 = k4 + IIf(dic(Left(itm, Len(itm) - 4) & "~C43") <> dic(Left(itm, Len(itm) - 4) & "~C44") Or dic(Left(itm, Len(itm) - 4) & "~C43") = 0 Or dic(Left(itm, Len(itm) - 4) & "~C44") = 0, dic(Left(itm, Len(itm) - 4) & "~Count"), 0)
        ElseIf Right(itm, 4) = "~C43" Then
            K3 = K3 + IIf(dic(itm) = dic(Left(itm, Len(itm) - 4) & "~C44") And dic(itm) <> 0, dic(Left(itm, Len(itm) - 4) & "~Count"), 0)
        Else
            K = K + IIf(dic(itm) = 0, 1, 0)
        End If
    Next itm

    Sheets("Total").Range("C41").Value = K
    Sheets("Total").Range("C42").Value = KK
    Sheets("Total").Range("C43").Value = K3
    Sheets("Total").Range("C44").Value = k4
    K = 0
    KK = 0
    K3 = 0
    k4 = 0

End Sub

1620027665161.png
 
Upvote 0
The code is also picking up this line as 'OK'

Book1.xlsm
FGHIJKLMN
2screwcaseghFinishTotal per setTotal per casePDMKS & DVResult
127225 01STG111 NOK
137225 01Final11 NOK
217225 01STG111 NOK
267225 01STG111 NOK
397225 01STG111 NOK
647225 01STG111 NOK
Sheet7


Since when you filter to "Final" there's only 1 entry of that, and columns J/K matches.

i was expecting 2 and 32 as result.. it gave me 4 and 30..
When you delete some rows, you also reduce the "Total per set", this may in turn result to some rows' column J sum now matching column K number.
 
Upvote 0
The code is also picking up this line as 'OK'

If the code is picking up first line as OK, then the total should be increased by one line..

VBA Code:
Set rRngVisible = .Offset(1).Resize(.Rows.Count [B]- 1[/B]).SpecialCells(xlCellTypeVisible)

Is this part of the code (-1) removing the last line from the count instead of removing the header ? i just had a doubt

Since when you filter to "Final" there's only 1 entry of that, and columns J/K matches.

in sample sheet, there are two entries like that right ?

row number 4 and row number 27

When you delete some rows, you also reduce the "Total per set", this may in turn result to some rows' column J sum now matching column K number.
ah yes, ur right.. understood this part.. thanks..

I had to calculate ok / nok again after deleting 3 rows.. instead of expecting 2 and 32..

Manual calc shows the answer should have been 3 and 31 instead of 4 and 30..

if the header is counted as OK line.. that should not reduce 31 to 30 right ?
 
Upvote 0
VBA Code:
Set rRngVisible = .Offset(1).Resize(.Rows.Count [B]- 1[/B]).SpecialCells(xlCellTypeVisible)

I just removed (-1) from this code.. it is counting header as OK line and the NOK values are showing correctly
 
Upvote 0
There is nothing wrong with the -1. It should be ignoring the header row as expected. To understand better, you should break it into parts and see what range they represent.
VBA Code:
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        Debug.Print .Address
        Debug.Print .Offset(1).Address
        Debug.Print .Offset(1).Resize(.Rows.Count - 1).Address

in sample sheet, there are two entries like that right ?

row number 4 and row number 27
There is also row 13, like I shown on my previous post. Your formula is probably wrong, marking it as "NOK", but if you filter to only "Final" rows, row 13 should be "OK".
 
Upvote 0
There is nothing wrong with the -1. It should be ignoring the header row as expected. To understand better, you should break it into parts and see what range they represent.
VBA Code:
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        Debug.Print .Address
        Debug.Print .Offset(1).Address
        Debug.Print .Offset(1).Resize(.Rows.Count - 1).Address


There is also row 13, like I shown on my previous post. Your formula is probably wrong, marking it as "NOK", but if you filter to only "Final" rows, row 13 should be "OK".

God.. I feel stupid.. lol

I have no words to thank you enough..

your code is perfect, my calculation was wrong as i assigned OK/NOK before and then counted after the filter..
should have set filter first and then counted OK/NOK..

My good man, I owe you a drink.. thanks a ton.. ? ??
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,007
Members
448,935
Latest member
ijat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top