UDF that checks if a cell contains a period.

  • Thread starter Thread starter Jonathan Brown
  • Start date Start date
J

Jonathan Brown

I have a User Defined function that counts the cells in a range that meet a
certain criteria. The criteria are:

1. does not contain a formula
2. is not a number
3. is not blank
4. contains a period.

I can get it to figure out the first three criteria, but I can't figure out
the expression to check to see if the cell contains a period.

Here's what my code looks like:

Function Jonathan(rng As Range) As Long

Dim c As Range

For Each c In rng

If c.HasFormula = False And c.Value <> "" And Not IsNumeric(c.Value)
And _
c.Value = "*.*" Then

Jonathan = Jonathan + 1

End If

Next

End Function

It seems to be taking the c.value = "*.*" as a string literal and there
doesn't seem to be a VBA equivalent to the .Net version of the .contains()
method. Any ideas would be greatly appreciated.

Thanks.
 
VBA has its own instr function:
if instr(1,c.value, ".", vbtextcompare) > 0 then
'it has a dot
...

Or you could use Like:
if c.value like "*.*" then

(I'd use instr.)
 
Hi

This should do it:

If c.HasFormula = False And c.Value <> "" And Not IsNumeric(c.Value)
And _
InStr(1, c.Value, ".") Then

Regards,
Per
 
The only thing I would add to the other posts is the you do NOT have to
check if the cell is empty... you are requiring the cell's text to have a
period; so if the cell's text has that character in it, then, by definition,
it can't be empty (so the test is not needed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top