Excel vba Variant array lookup -


i have 2 (non empty) arrays (of variants) numbers. list data in first array , not in second array.

dim existingworkerids() variant dim newworkerids() variant    each temp in newworkerids          if existingworkerids.contains(temp)             ...do sth...         end if     next temp 

is possible?

easily doable abusing match.

the first procedure test verification, , example of how things have declared (and conversely, declarations have change if need other variable types, etc).

sub testcaller()     dim testarr1() variant ' <~~ variable type must match     dim testarr2() variant '     variable required in      dim testarr3() variant '     actual procedure     dim testarr4() variant       testarr1 = array("abc", "abc", "def", "abc", "asdf", "bcd")     testarr2 = array("abc", "asdf")     call listuniquearraycontents(testarr1(), testarr2())      testarr3 = array(1, 2, 3, 4, 5)     testarr4 = array(1, 2)     call listuniquearraycontents(testarr3(), testarr4()) end sub  sub listuniquearraycontents(arr() variant, arrcompare() variant)     dim uniquevalues() variant     dim mindex variant     dim j integer      j = 0      = 0 ubound(arr())         ' reset placeholder our match values         mindex = null          ' disable errors, otherwise popups every time there's unique value         on error resume next          ' call match function         mindex = application.worksheetfunction.match(arr(i), arrcompare(), 0)          ' restore normal error handling         on error goto 0          if mindex < 1 or isnull(mindex)             ' if match variable null, means value unique             ' we'll write value separate array keep track of             if j = 0 redim preserve uniquevalues(0 0)             if j <> 0 redim preserve uniquevalues(ubound(uniquevalues()) + 1)             uniquevalues(ubound(uniquevalues)) = arr(i)             j = j + 1         end if     next      debug.print "--unique values:--"     k = lbound(uniquevalues()) ubound(uniquevalues())         debug.print uniquevalues(k)     next k     debug.print "--end--" end sub 

which, test examples, gives expected:

--unique values:--
def
bcd
--end--
--unique values:--
3
4
5
--end--

alternatively, can change function , have return array of unique values.

change this:
sub listuniquearraycontents(arr() variant, arrcompare() variant)

to this:
function listuniquearraycontents(arr() variant, arrcompare() variant) variant

and replace last-most for-loop listuniquearraycontents = uniquevalues()


Comments