CountIf unable to evaluate value in cell

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")
upobDaPlayaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
upobDaPlayaAuthor 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 ?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
upobDaPlayaAuthor 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,
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Yes. * wild card will only look for the stuff other than the criteria. So if your criteria is "09.09.2019" and the cell has "09.09.2018" in it, the count will be zero because the whole criteria must exist in the column in any cell.

I used wild card to encounter any leading or trailing spaces in the cells. If you are sure that there are no leading or trailing spaces in the column, you can skip the wild card and just use the criteria strCk alone.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
upobDaPlayaAuthor Commented:
ok... i will try in the morning...
0
upobDaPlayaAuthor Commented:
Spot on !
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Thanks for the feedback!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.