Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on 

Count # of rows in a range that meet two criteria using VBA

I need to count the number of rows in the range:

shtGroups.Range("A1:B50")

where the value in column A = lngGroup and the value in column B = lngSub

I figured out how to do this in a cell formula, but I need to do it in vba.

Microsoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Dale Fye
Avatar of Norie
Norie

You could try Evaluate, where you can basically use your worksheet function.

If lngGroup and lngSub are numeric

result = Evaluate("SUMPRODUCT(--(A1:A50=" & lngGroup & "), --(B1:B50=" & lngSub & "))")

If they are strings:

result = strformula = "SUMPRODUCT(--(A1:A50=""" & lngGroup & """), --(B1:B50=""" & lngSub & """))"


Avatar of Metallimirk
Metallimirk
Flag of United States of America image

OK, try this formula:

Range("A1:A" & Cells(65536, "A").End(xlUp).Row & ",B1:B" & Cells(65536, "A").End(xlUp).Row).Select



ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

ASKER

imnorie,

It worked as soon as I set the focus to shtGroup via Activate and removed the extra quotes for a numeric value.

What is the point of the double negatives "--" at the beginning of each range?
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo