Hello,
I am trying to create a macro to copy/paste a generated vLookup formula using a user selected range ("Reference Data") and with the following criteria:
- The table array starting column needs to increment by 1 until it reaches the 2nd to last column of the user selected range, but the ending column needs to remain locked
- The formula should be copied left to right while decrementing the Column_index (starting with the first column of the user selected range ending with the 2nd to last column of the user selected range). This is so that the Column_index is looking at the same column of the reference data
- This should repeat for each row in the "XYR Data" Table
To do this manually, I first write the vLookup as
and then lock rows and columns to get
This is then flash filled across however many columns are in the reference data less one.
I then go through each filled cell and decrement the Col_Index_Num by 1 until it gets down to 2.
This entire row is then flash filled down for each row of the "XYR Data" table.
I've gotten the code this far. It asks or the reference range and target cell, and generates a vlookup based off of that. But the copy paste is happening in reverse order to what I need and I havent been able to figure out how to lock certain Row/Column references in the formula.
This is ideally how the final table of formulas would look and how the lookup range would move with each step
And what the final Results would show
The final cleanup of deleting the #N/A cells and getting the results in column E is done in a separate macro that already works.
Some final information:
The XYR is always in that format, with the first lookup value in cell D2 and the final results ultimately being in column E.
The reference data is a variable number of columns wide but the Index Column is always the very last one.
Any help would be appreciated!
I am trying to create a macro to copy/paste a generated vLookup formula using a user selected range ("Reference Data") and with the following criteria:
- The table array starting column needs to increment by 1 until it reaches the 2nd to last column of the user selected range, but the ending column needs to remain locked
- The formula should be copied left to right while decrementing the Column_index (starting with the first column of the user selected range ending with the 2nd to last column of the user selected range). This is so that the Column_index is looking at the same column of the reference data
- This should repeat for each row in the "XYR Data" Table
To do this manually, I first write the vLookup as
Excel Formula:
=VLOOKUP(D2,I2:L6,4,FALSE)
Excel Formula:
=VLOOKUP(D$2,I$2:$L$6,4,FALSE)
I then go through each filled cell and decrement the Col_Index_Num by 1 until it gets down to 2.
This entire row is then flash filled down for each row of the "XYR Data" table.
I've gotten the code this far. It asks or the reference range and target cell, and generates a vlookup based off of that. But the copy paste is happening in reverse order to what I need and I havent been able to figure out how to lock certain Row/Column references in the formula.
VBA Code:
Sub GenerateVLOOKUPAndCopy()
Dim SourceRange As Range
Dim NewRange As Range
Dim TargetCell As Range
Dim FormulaString As String
Dim ColumnIndex As Integer
Dim LookupValueColumn As Integer
' Check if a range is selected
On Error Resume Next
Set SourceRange = Application.InputBox("Select the BOM range:", Type:=8)
On Error GoTo 0
If SourceRange Is Nothing Then
MsgBox "No BOM selected. Exiting the macro."
Exit Sub
End If
' InputBox for the target cell where the VLOOKUP formula will be pasted
On Error Resume Next
Set TargetCell = Application.InputBox("Select the target cell where the VLOOKUP formula should be pasted:", Type:=8)
On Error GoTo 0
If TargetCell Is Nothing Then
MsgBox "No target cell selected. Exiting the macro."
Exit Sub
End If
' Initialize column index and lookup value column
ColumnIndex = SourceRange.columns.Count
LookupValueColumn = 1
' Loop to generate and copy VLOOKUP formulas
Do While ColumnIndex >= 2
' Generate the VLOOKUP formula
FormulaString = "=VLOOKUP(" & TargetCell.Offset(0, -1).Address & "," & _
SourceRange.Address(True, False) & "," & _
ColumnIndex & "," & _
False & ")"
' Paste the formula in the target cell
TargetCell.Offset(0, ColumnIndex - 2).Formula = FormulaString
' Decrement the column index and increment the lookup value column
ColumnIndex = ColumnIndex - 1
LookupValueColumn = LookupValueColumn + 1
Loop
End Sub
This is ideally how the final table of formulas would look and how the lookup range would move with each step
And what the final Results would show
The final cleanup of deleting the #N/A cells and getting the results in column E is done in a separate macro that already works.
Some final information:
The XYR is always in that format, with the first lookup value in cell D2 and the final results ultimately being in column E.
The reference data is a variable number of columns wide but the Index Column is always the very last one.
Any help would be appreciated!