excel - VBA: I need to write both a function and a calling sub to do the following -
in main subroutine, have 2 user inputs ((1) range address (e.g., a1:c50), (2) name string (e.g., james)), , call function subroutine (by passing inputs arguments), , printout result through message box whether name exists or doesn't exist in range.
both search range , name should input users. how write function subroutine , calling sub? have far.
function nameexists(name string, area range) boolean if name = area.value nameexists = true else nameexists = false end if end function sub main() dim nameexists boolean dim name string name = inputbox("enter name") area = inputbox("enter range") if nameexists = true msgbox name & " has been found" else msgbox name & " has not been found" end if end sub
you need call function checks if exists , pass name , area variables you've had user input. here crude example:
sub main() dim nm string dim ar string nm = inputbox("enter name") ar = inputbox("enter range") if nameexists(nm, ar) = true msgbox nm & " has been found" else msgbox nm & " has not been found" end if end sub private function nameexists(name string, area string) boolean dim myrange range set myrange = range(area) each mycell in myrange if mycell.value = name nameexists = true exit end if next set myrange = nothing end function