I have a spreadsheet table that contains some key-value pairs but in a set for example as shown below (the order of key values is not fixed):
Key Value Key Value Key Value
ABC 999 JKL 888 MNO 777
DEF 555 RST 666 XYZ 444
AAA 123 NNN 333
I am using a mapping sheet that contains the description with reference to the Key and using that key I am trying to print the value next to the description in a separate sheet.
My Mapping sheet looks like this:
SNo Key Description
1 ABC Test1
2 JKL Test2
3 MNO Test3
4 DEF Test4
5 RST Test5
6 XYZ Test6
7 AAA Test7
8 BBB Test8
9 CCC Test9
... .... .....
14 NNN Test14
In the output, I'm trying to display those keys which are present in my input sheet next to their SNo & description. Here's the expected output that I am working on getting.
SNo Description Value
1 Test1 999
2 Test2 888
3 Test3 777
4 Test4 555
5 Test5 666
6 Test6 444
7 Test7 123
14 Test14 333
How do I achieve this in a excel Macro?
I have a function which I'm using to get the value based on the cell address in the selected sheet and then storing it in a variable and passing it to the output. But that is a cell by cell approach and will take too long to execute.
Here's the sample function that I used to get the value based on the cell address that currently requires manipulation of column & row indices to look up for values & give me the cell address which I can utilize to bring cell value:
Function getAddress(ByVal colValue As String, ByVal rowValue As String) As Range
Dim row, col As Variant
With ActiveSheet
row = Application.Match(rowValue, .Columns("B"), 0) 'To lookup for key in the mapping sheet but need to be changed based on the sheet the function is doing a lookup in
col = Application.Match(colValue, .Rows(1), 0)
If IsError(r) Then
row = 0
col = 0
End If
Set getAddress = .Cells(row, col)
End With
End Function
I'm sure a much better way exists, but I'm exploring VBA for the last couple of months and learning it on the go. Any help is appreciated in advance.




