vba - Check whether any cells in a given range have a value equal to the string -


how create boolean function searches cells in range , checks if have value equal specified string? if so, returns true. if not, returns false.

this far i've gotten

function nameexist(byval name string, namerange range) boolean boolean nameexist = false if (name = michael) 

here's 1 implementation:

function nameexists(byval searchname string, namerange range) boolean     nameexists = not namerange.find(what:=searchname, lookat:=xlwhole) nothing end function 

here's another, more manual / brute force one:

function nameexists(byval searchname string, namerange range) boolean     dim long     dim j long     dim v variant     v = namerange.value     nameexists = false     = 1 ubound(v, 1)         j = 1 ubound(v, 2)             if v(i, j) = searchname                 nameexists = true                 exit function             end if         next j     next end function 

both should give same results.

example usage:

if nameexists("michael",range("b3:c6")) msgbox "michael exists!" 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -