[VBA] Countif with For loop

HaHa1712

New Member
Joined
Jan 12, 2018
Messages
2
How to transform the code below into VBA Code (or another method)?
IF(COUNTIF(Data!$1:$1,A2)=0,0,COUNTIF(Data!$1:$1,B1))+
IF(COUNTIF(Data!$2:$2,A2)=0,0,COUNTIF(Data!$2:$2,B1))+
IF(COUNTIF(Data!$3:$3,A2)=0,0,COUNTIF(Data!$3:$3,B1))+
.
.
.
IF(COUNTIF(Data!$10000:$10000,A2)=0,0,COUNTIF(Data!$10000:$10000,B1))

Thanks.
:)

 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Not sure what exactly you are asking for.

If what you need is a code that checks if a sheet named Data has any values in row 1:10000 that matches A2 (in another sheet) and if said is true, sums the counts of matches to B1, then try this:
Note: I assume that the values A2 and B1 in your formulas are static values in a sheet called "LOOKUP". Change accordingly.

Code:
Sub Test()


Dim num As Long


num = 0


For i = 1 To 10000
    If Application.WorksheetFunction.CountIf(Sheets("Data").Range("$" & i & ":$" & i), Sheets("LOOKUP").Cells(2, 1).Value) = 0 Then
        GoTo Cont
    Else
        n = n + Application.WorksheetFunction.CountIf(Sheets("Data").Range("$" & i & ":$" & i), Sheets("LOOKUP").Cells(1, 2).Value)
    End If
Cont:
Next i


End Sub

Code not tested and I am afraid it might take some time to compute for 10,000 rows...
 
Upvote 0
Seems I was a tad too quick with the code :)

Code:
Sub Test()


Dim num As Long


num = 0


For i = 1 To 10000
    If Application.WorksheetFunction.CountIf(Sheets("Data").Range("$" & i & ":$" & i), Sheets("LOOKUP").Cells(2, 1).Value) = 0 Then
        GoTo Cont
    Else
        num = num + Application.WorksheetFunction.CountIf(Sheets("Data").Range("$" & i & ":$" & i), Sheets("LOOKUP").Cells(1, 2).Value)
    End If
Cont:
Next i


MsgBox num


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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