1

How can I use the COUNTIF() function to count only certain text strings that exist in the range?

I tried to use the below, but I get an error of

Syntax error

This is the syntax I attempted

Dim worksheetmaster As String = "Master"
Dim worksheettocheck As String = "New"
Dim softcount As Int, i As Long, hardcount As Int

softcount = Evaluate("=COUNTIF(Range('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Soft")")
hardcount = Evaluate("=COUNTIF(Range('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Hard")")

EDIT
I tried to use this syntax without the Range and am still getting the error

hardcount = Evaluate("=COUNTIF('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Hard"")
10
  • you dont need the range, it will be as it would in a worksheet. Commented Mar 18, 2017 at 0:12
  • @Nathan_Sav - still got an error :( -- see my edit. Commented Mar 18, 2017 at 0:14
  • no , after each segment like =countif('" & w1 & "'!A:A,"'" & w2 & "!A:A"..... Commented Mar 18, 2017 at 0:15
  • @Nathan_Sav - que? Commented Mar 18, 2017 at 0:16
  • countif is (range,value) msdn.microsoft.com/en-us/library/office/ff836633.aspx Commented Mar 18, 2017 at 0:18

2 Answers 2

1

To match in column A with Hard in column B, this is how it should be:

hardcount = Application.Evaluate("COUNTIFS('" & worksheettocheck & "'!A:A,'" & worksheetmaster & "'!A" & i & ",'"  & worksheettocheck & "'!B:B, ""Hard"")")

softcount = Application.Evaluate("COUNTIFS('" & worksheettocheck & "'!A:A,'" & worksheetmaster & "'!A" & i & ",'"  & worksheettocheck & "'!B:B, ""Soft"")")
Sign up to request clarification or add additional context in comments.

1 Comment

I am wanting to get a count where column A on Master matches Column A on new - but the value in Column B is Hard. If I try your second syntax above it always gives a 0 result returned even thought it should be more.
0

Your first syntax errors are here:

Dim worksheetmaster As String = "Master"
Dim worksheettocheck As String = "New"

You can't do that. Instead, you would need to use:

Dim worksheetmaster As String
Dim worksheettocheck As String
worksheetmaster = "Master"
worksheettocheck = "New"

Even better would be to assign them to point diectly at the worksheets, but let's work with your code as much as possible instead of totally rewriting it.

For the countif, you cannot join ranges that way. You haven't even assigned a value to i, but assuming i = 1, your code:

softcount = Evaluate("=COUNTIF(Range('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Soft")")

evaluates to total nonsense in VBA:

softcount = Evaluate(=COUNTIF(Range('New'!A:A'Master'!A1)Soft))

Now, from what I can tell, what you are trying to do is to count how many times the value in cell Master!A & i appears in the range New!A:A, depending whether Master!A & i="Soft" or Master!A & i="Hard". So let's see if we can find code that will do that.

For data we enter "Soft" into Master!A1 and "Hard" into Master!A2. Then we enter random "Soft" or "Hard" into various cells in the column New!A:A.

Now your code looks like this:

Dim worksheetmaster As String
Dim worksheettocheck As String
Dim softcount As Long, i As Long, hardcount As Long

worksheetmaster = "Master"
worksheettocheck = "New"
i = 1
softcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("A" & i).Value)
i = 2
hardcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("A" & i).Value)

This is inefficient and limited, but it retains as much of your original code as possible, and it works.

Edited to add: if "Hard" is in Master!B & i instead of A & i then the code becomes:

i = 1
softcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("A" & i).Value)
hardcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("B" & i).Value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.