Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 440999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:55:43+00:00 2026-05-12T20:55:43+00:00

In Excel, I’m writing a custom function in VBA that needs to take a

  • 0

In Excel, I’m writing a custom function in VBA that needs to take a criteria string and criteria range like the built-in SUMIF function. Does Excel expose the functionality to test a criteria string anywhere in its API or do I have to write it myself?

In case it’s relevant, I’m writing a “CountUniquesIf” formula, that counts the unique values in a range if they meet a criterion. This is what I have so far.

Function CountUniquesIf(CondRange As Range, Criteria As String, _
    Range As Range) As Long

    Static dict As New Scripting.Dictionary
    Dim index As Long

    index = 1
    For Each Cell In Range.Cells
        If CondRange(index).Value = Criteria And Cell.Value <> "" Then
            dict(Cell.Value) = Empty
        End If
        index = index + 1
    Next Cell

    CountUniquesIf = dict.Count
    dict.RemoveAll
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:55:43+00:00Added an answer on May 12, 2026 at 8:55 pm

    You can actually do the whole thing with just regular formulas if you want.

    Please see:

    http://www.officearticles.com/excel/count_unique_values_in_microsoft_excel.htm

    or

    http://office.microsoft.com/en-us/excel/HP030561181033.aspx

    You will need to modify the formula slightly, though, to cover the “if” part of your scenario:

    =SUM(IF(FREQUENCY(IF((LEN(A1:A15)>0)*(B1:B15=D4),MATCH(A1:A15,A1:A15,0),""), IF((LEN(A1:A15)>0)*(B1:B15=D4),MATCH(A1:A15,A1:A15,0),""))>0,1))
    
    Where A1:A15 is your Range, B1:B15 is your CondRange, and D4 is your Criterion.
    

    Remember to enter this as an array formula (paste the formula and press Ctrl-Shift-Enter instead of just Enter).

    That said, I think your VBA formula is a good solution too (probably more user-friendly than creating a monster array formula every time you need this type of count).

    Update

    Given your clarification, I really don’t think there’s a built-in “criterion analyzer”, but I don’t think it would be too difficult to enhance your formula to cover the different possible criteria. That way, your CountUniquesIf formula will really do what people think it does. Specifically, you could do a little parsing that checks for all the possible operators (is there anything besides “=”, “>”, “>=”, “<“, “<=”?) that could be prefixed before the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer django-grappelli provides this as one of the features. Here's the… May 12, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Javascript is the only language the works well on the… May 12, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Not possible. That's a client side setting which is in… May 12, 2026 at 10:09 pm

Related Questions

In Excel, I'm writing a custom function in VBA that needs to take a
In excel 2007, I have a formula in a cell like the following: =COUNTIFS('2008-10-31'!$C:$C;>=&'$A7)
In Excel 2003, when I change Series.Interior.ColorIndex to a value I need, it has
In Excel (2007) VBA how do I print the worksheet to a specific printer
In Excel 2007, I have a single dataset in the form of an Excel

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.