vba - How can I check if a string only contains letters? -
i'm using function allows me review string of text , evaluate if composed of letters. housed in module called "general". general module exists house public functions , variables. function code listed below:
public function isalpha(strvalue string) boolean dim intpos integer intpos = 1 len(strvalue) select case asc(mid(strvalue, intpos, 1)) case 65 90, 97 122 isletter = true case else isletter = false exit end select next end function
next have 2 "if" routines evaluate first 2 characters of textbox in userform. first routine asks if 1st character numeric , second routine asks if 2nd character alpha. currently, second "if" routine ejecting me sub-routine when isalpha tests true, rather generating msgbox. isalpha function not being called correctly?
if routines code listed below:
private sub cmdmap_click() txtdxcode if isnumeric(left(me.txtdxcode.text, 1)) msgbox "incorrect dx code format entered. ", vbexclamation, "dx code entry" txtdxcode.value = "" txtdxcode.setfocus exit sub end if if isalpha(left(me.txtdxcode.text, 2)) msgbox "incorrect dx code format entered. ", vbexclamation, "dx code entry" txtdxcode.value = "" txtdxcode.setfocus exit sub end if end
why don't use regular expressions instead? there's no loops involved:
public function isalpha(strvalue string) boolean isalpha = strvalue worksheetfunction.rept("[a-za-z]", len(strvalue)) end function
also, when create user-defined function (udf) need ensure return value assigned name of actual function, in case isalpha
- not isletter
otherwise value never passed back.
Comments
Post a Comment