Re: Combine Two Excel Lists
Hi. Firstly this topic should be in the "Office development" section of the forum.
I have done some spreadsheets myself and if you have variable names in a cell you should use named ranges to label specific cells or area's. You can then use these ranges in code to refer to a certain cell or cells regardless of content. For example:
worksheets("Sheet1").Range("Column_A_Header")
You can also use dynamic named ranges to refer to the list of items under the header. For example, define a named range to point to the following cells:
Code:
=offset($a$2,0,0,counta(a:a)-1)
This will select all the cells in column A which are not empty, and which are in cell A2 or lower.
I would also like to point out that while VBA is possible, it is perfectly possible to do matching like this using just excel formulas:
Code:
'use code like this to select the matrix and copy it to a fixed location. This location will be referred to using formulas.
Worksheets("Sheet1").Range("A1:D4").Copy _
destination:=Worksheets("Sheet2").Range("E5")
Create a dynamic named range containing all the rows and columns which contain the values of the matrix.
For this example we will assume that:
- There is a named range "Matrix" which contains the matrix values.
- (Datalist)Offercode is in cell H2.
- (Matrix)Offercode is in the first column of the range "Matrix".
Next, fill cell i2 with the formula:
Code:
=index("Matrix", match($h2, offset("Matrix",0,0,rows("Matrix")),0),2)
Notice that the H has a $ in front of it, the number does not. This is needed when copying the formula to other cells.
The match function will search column I for the offercode in H2 and return the rownumber, counting from the starting row of Matrix. The index function will return the value of the cell to the right of the matching offercode.
Copy this formula down.
To copy this formula to the right (Matrix)Date, the Index function should refer to the next column:
Code:
'last number incremented
=index("Matrix", match($h2, offset("Matrix",0,0,rows("Matrix")),0),3)
I hope this helps you out, have fun with it!