View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Match based on a lookup and then flag

formulated the question :
The line of code in the For next loop
What exactly is this doing?
vD1(j, 2) = vD1(j, 3): vD1(j, 3) = ""


This line is preparing the output data in the original (Sheet1) array.
It puts col3 into col2 position because col2 data is not needed AND
that's where you want col3 data to be when it gets put in Sheet3. It
then inserts an empty string into col3 position so it can be filled
with data from Sheet2's array col3 data.


And in the double loop through both sheet1 and sheet2, I guess I am
not following that either. The (k,2) and (j,1) refer to dimensions -
same as columns in this case on the 2 sheets??


This double loop iterates Sheet2's array (outer loop) to extract its
col3 data into Sheet1's array (inner loop) col3 position. To ensure
unique values only get inserted, the inner loop checks if Sheet1's
array, col2 data matches Sheet2's array col1 data. If it matches AND
only if Sheet1's array col3 contains an empty string will it put
Sheet2's col3 data into the array and exit the (inner) loop. Next
round, the outer loop moves to the next row of Sheet2's data and the
cycle for the Sheet1 array repeats. Since it will again find the same
match as the first loop, this is skipped because Sheet1's array col3 no
longer contains an empty string and so it continues to find the next
match.

As for the array dimensions, when we load Excel ranges into a variant
as done here it results in a 2D array. In the case of your sample data
this is an 8 row by 3 column array. So...

Dim vD1 'creates a variant where we put the data from Sheet1!$A1:$C8
This is now vD1(8, 3), a 2D 1-based array
This could also be dimmed vD1(1 To 8, 1 To 3)
The reason for 1-base is due to the way rows/cols are numbered.
(they start at 1)

This can be confusing at first because this is not the normal case for
working with conventional (0-based) arrays. The array created by Excel
ranges can be thought of in terms of R#C#, where the # sign is filled
with the row/col count in the range. Thus, element 1,1 of your 8x3
example contains the value in cell A1; 1,2 contains the value in B1,
and so on. So element 8,1 contains the value in A8; element 8,2
contains the value in B8, and so on.

HTH

--
Garry

Free usenet access at
http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc