We help IT Professionals succeed at work.

CountIf unable to evaluate value in cell

153 Views
Last Modified: 2018-10-12
I have a VBA COUNTIF statement that will not evaluate a value within a cell unless I go into the call and re-type the cell.  The cell value is text.  As an example in the cell is 10.10.2018.  If I go into the cell and re-type in the value (note I was sent the spreadsheet, I did not type the original value).

When I set a variable to the cell it correctly displays "10.10.2018" (before any re-type.  Thus, why am I forced to re-type in the value in order for the CountIf to recognize the value ?

 Count = Application.CountIf(Cells(5, i), "10.10.2018")
Comment
Watch Question

Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
Since you are not using a wild card in the criteria, the Countif will count if there is an exact match. Maybe the cell contains a leasing or trailing spaces and when you retype you just type the value and it counts correctly.

You may try Countif with a wild card like this...
Count = Application.CountIf(Cells(5, i), "*10.10.2018*")

Open in new window

Or if you just want to check if there is a text "10.10.2018" in the cell, you may try something like this...
If InStr(Cells(5, i), "10.10.2018") > 0 Then
    'do something
End If

Open in new window

Or
Count = InStr(Cells(5, i), "10.10.2018")

Open in new window

Author

Commented:
I need to check all cells in a column and if any cell in the column does not equal my variable such as strCk where strCk = '10.10.2018'
then I will stop the code..

I thought the CountIF may be most efficient ?
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
In that case you may try something like this...

'Say you want to check the occurrence of variable strCk in Column A
If Application.CountIf(Columns("A"), "*" & strCk & "*") > 0 Then
    'do other stuff here
Else
    Exit Sub
End If

Open in new window

If you have a variable which holds the column number to check, you may replace "A" with the variable.
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
Or just exit the sub if no value is found in the column like...

If Application.CountIf(Columns("A"), "*" & strCk & "*") = 0 Then    
    Exit Sub
End If

Open in new window

Author

Commented:
Isnt * a wildcard ?  if Column A has "09.09.2018" and strCk has "09.09.2019" would the Count be 0 ?  That would be the hope I desire,
Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
ok... i will try in the morning...

Author

Commented:
Spot on !
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
Thanks for the feedback!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions