HH_Captain
New Member
- Joined
- May 25, 2021
- Messages
- 2
- Office Version
- 365
- 2019
- Platform
- Windows
Dear Colleagues,
I want to Browse a file and give path to the VBA code over the Index match formula. Basically, the below formula for index match works within 2 sheets in the same workbook. I need to have option to source the data for index match with an option to open file and consider data from sheet(1) of that file which is opened.
Below is the code of VBA:
Sub MacroLynx()
Dim x As Integer, Y As Integer
Dim SourceRange As Range
Dim Lookrange As Range
Dim Header_Range As Range
Dim ws As Worksheet
Set wsd = Sheets("FARE (2)") 'I want to replace wsd with the Open data file code
Set WSVO = Sheets("FARE")
Set SourceRange = wsd.Range("A:G")
Set Lookrange = wsd.Range("A:A")
Set Header_Range = wsd.Range("1:1")
WSVO.Select
MyLastRow = WSVO.Cells(Rows.Count, 1).End(xlUp).Row
MyLastColumn = WSVO.Cells(6, Columns.Count).End(xlToLeft).Column
For x = 7 To MyLastRow
For Y = 2 To MyLastColumn
On Error Resume Next
WSVO.Cells(x, Y) = WorksheetFunction.Index(SourceRange, _
WorksheetFunction.Match(WSVO.Cells(x, 1), Lookrange, 0), _
WorksheetFunction.Match(WSVO.Cells(6, Y), Header_Range, 0))
Next Y
Next x
On Error GoTo 0
End Sub
I want to Browse a file and give path to the VBA code over the Index match formula. Basically, the below formula for index match works within 2 sheets in the same workbook. I need to have option to source the data for index match with an option to open file and consider data from sheet(1) of that file which is opened.
Below is the code of VBA:
Sub MacroLynx()
Dim x As Integer, Y As Integer
Dim SourceRange As Range
Dim Lookrange As Range
Dim Header_Range As Range
Dim ws As Worksheet
Set wsd = Sheets("FARE (2)") 'I want to replace wsd with the Open data file code
Set WSVO = Sheets("FARE")
Set SourceRange = wsd.Range("A:G")
Set Lookrange = wsd.Range("A:A")
Set Header_Range = wsd.Range("1:1")
WSVO.Select
MyLastRow = WSVO.Cells(Rows.Count, 1).End(xlUp).Row
MyLastColumn = WSVO.Cells(6, Columns.Count).End(xlToLeft).Column
For x = 7 To MyLastRow
For Y = 2 To MyLastColumn
On Error Resume Next
WSVO.Cells(x, Y) = WorksheetFunction.Index(SourceRange, _
WorksheetFunction.Match(WSVO.Cells(x, 1), Lookrange, 0), _
WorksheetFunction.Match(WSVO.Cells(6, Y), Header_Range, 0))
Next Y
Next x
On Error GoTo 0
End Sub