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
 
AutoFilter Field:=9, Criteria1:="Final" while setting the range so that any calculating taking place is already defined by visible rows after auto filter..
Can you show your code that includes this filtering portion?

When I manually filter to "Final" on that data sample, I get 3 OK and 34 NOK with the ".SpecialCells(xlcelltypevisible)" added.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Can you show your code that includes this filtering portion?

When I manually filter to "Final" on that data sample, I get 3 OK and 34 NOK with the ".SpecialCells(xlcelltypevisible)" added.

i used the below code..

VBA Code:
 Set dic = CreateObject("scripting.dictionary")
    dic.CompareMode = vbTextCompare
    Set rng1 = Sheets("Screwpoints").Range("A2").CurrentRegion
    rng1.AutoFilter Field:=29, Criteria1:="Final", Operator:=xlFilterValues
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        Set rRngVisible = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    End With
 
Upvote 0
You don't need another range variable to do the filtering, you can combine like this:

VBA Code:
    Set dic = CreateObject("scripting.dictionary")
    dic.CompareMode = vbTextCompare
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        .AutoFilter Field:=9, Criteria1:="Final", Operator:=xlFilterValues '<--  Just make sure the Field:=9 is pointing to the right column -- is it 9 or 29?
        Set rRngVisible = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    End With
 
Upvote 0
You don't need another range variable to do the filtering, you can combine like this:

VBA Code:
    Set dic = CreateObject("scripting.dictionary")
    dic.CompareMode = vbTextCompare
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        .AutoFilter Field:=9, Criteria1:="Final", Operator:=xlFilterValues '<--  Just make sure the Field:=9 is pointing to the right column -- is it 9 or 29?
        Set rRngVisible = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    End With

i get 3 and 71 still.,

1620015166003.png
 
Upvote 0
Can you add this line and see what is printed in the immediate window in VBA editor?

VBA Code:
    With Sheets("Screwpoints").Range("A2").CurrentRegion
        .AutoFilter Field:=9, Criteria1:="Final", Operator:=xlFilterValues
        Set rRngVisible = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        Debug.Print rRngVisible.Address '<--
    End With
 
Upvote 0
Can you add this line and see what is printed in the immediate window in VBA editor?

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

i got below error

1620020067725.png
 
Upvote 0
i got below error
How did you get it to run previously?

Add this line too:
VBA Code:
    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
 
Upvote 0
How did you get it to run previously?

Add this line too:
VBA Code:
    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

I'm stumped.. rebooted system, cleared %temp% & recent.. still getting same error.. earlier it ran without any errors!

1620021029755.png
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,289
Members
449,498
Latest member
Lee_ray

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