Excel VBA. Compare if all letters in a string is in another string -
i compare if string has characters of string.
example:
string 1 hejslclo
string 2 hello
true string 2 has characters of string one.
i have turned string 1 array , think there should loop check using instr
my first attempt @ this:
sub stringintoarray() dim temp string dim mystring string dim string2 string mystring = "heaslsflo" string2 = "hello" temp = strconv(mystring, vbunicode) temp = left(temp, len(temp) - 1) aletter = split(temp, chr(0)) 'dim startrow integer dim integer = 0 l(temp) if instr(i, string2, aletter(i)) <> 0 msgbox ("yes") else msgbox ("no") end if next end sub
ok - got bored , bit. had @ code, there. think should want
option explicit public function comparestrings(string1 string, string2 string) boolean dim long, j long, k long dim arr1 variant, arr2 variant ' set default comparestrings = false ' split strings arrays arr1 = split(strconv(string1, vbunicode), chr(0)) arr2 = split(strconv(string2, vbunicode), chr(0)) ' initialise counter k = 0 ' loop on both arrays = lbound(arr2) ubound(arr2) j = lbound(arr1) ubound(arr1) if lcase(arr2(i)) = lcase(arr1(j)) k = k + 1 exit end if next j next ' test if counter equal array length , set answer if if k - 1 = ubound(arr2) comparestrings = true end function
public sub testcomparison() dim string1 string, string2 string string1 = "hello" string2 = "hejslclo" if comparestrings(string1:=string1, string2:=string2) debug.print string1; " contains "; string2 else debug.print string1; " not contain "; string2 end if end sub
Comments
Post a Comment