For a small project i want to use a vlookup or match in VBA to derive data from another sheet. I found it a difficult task even with the help of google I couldn't find the solution.
I divided the project into 3 phases:
- First phase is creating a working Vlookup in VBA (can't get it working)
- Search for the range where it needs to be executed (dynamic) (I think I managed)
- Loop through all the cells in the table (totally stuck on "For each" statement)
I managed to fetch the dynamic range #Firstcell and #Lastcell
But I'm really stuck at the loop and vlookup. I want to create the vlookup in such way that for each cell the X will be A & rownumber and Y will be Columnletter & "4". The vlookup needs to be executed from Firstcell to Lastcell.
Sub Match_Values()
' Variables
Dim X As Integer
Dim Y As Integer
Dim Firstcell As Integer
Dim Lastcell As Integer
' Range determination
With ActiveSheet
Range("A3").Select
Selection.End(xlToRight).Select
Firstcell = ActiveCell.Offset(1, 1).Range("A1").Select
End With
With ActiveSheet
Lastcell = ActiveCell.SpecialCells(xlLastCell).Select
End With
' For each cell create a vlookup with on rowindex en on y the columnindex loop statement
' Vlookup
With WorksheetFunction
c04 = .VLookup(X, [Pivot!A4:CC99], .Match(Y, [Sheet1!A4:CC4], 0), False)
End With
MsgBox c04
End Sub
Thanks in advance, if i need to provide additional feedback please let me know.
Edit, Thanks for the feedback I uploaded the example file: https://ufile.io/48evu (i'm sorry didn't see how to disclose in stackoverflow)