Jonnathanjons
Member
Below VBA script I have currently pull the results only if the search value column is in the first column. Example Emp ID to search should be in Column A.I would like the script to fetch the results for multiple columns based on header name irrespective of the position of the search header column. Please you may change the script to fit to run quicker as I have over 50 K search line from sheet2.This script works in the attached file if the first 3 columns are removed.
Sheet 1 is Search tab and Sheet 2 is the Raw "Data" which is the source. attached a working file
Sheet 1 is Search tab and Sheet 2 is the Raw "Data" which is the source. attached a working file
Sub IndexMatcharray()
Application.ScreenUpdating = False
Dim x As Integer, y As Integer
Dim ws As Worksheet
Dim strFile As String
Dim TableArray As Range, hRange As Range
Const m = "MESSAGE", S = "Data"
If Not Evaluate("ISREF('" & S & "'!A1)") Then
Sheets.Add(, ActiveSheet).Name = S
Application.StatusBar = "Add your search list in Sheet 'DATA' column A and Proceed!!": Exit Sub
Application.StatusBar = False
Else
Application.StatusBar = False
End If
Set wsED = ActiveWorkbook.ActiveSheet
Set hRange = wsED.Range("1:1")
Set rED = ActiveWorkbook.Sheets(S)
Set xRange = rED.Range("A:A")
Set hdxRange = rED.Range("1:1")
Set TableArray = rED.UsedRange.Columns
empid = WorksheetFunction.Match("EMP ID", hRange, 0)
ColPI = WorksheetFunction.Match("Port ID", hRange, 0)
ColDes = WorksheetFunction.Match("Designation", hRange, 0)
ColN = WorksheetFunction.Match("Name", hRange, 0)
ColLoc = WorksheetFunction.Match("Location", hRange, 0)
LastRow = wsED.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = wsED.Cells(1, Columns.Count).End(xlToLeft).Column
For x = 2 To LastRow
For y = 2 To LastCol
On Error Resume Next
wsED.Cells(x, y) = WorksheetFunction.Index(TableArray, _
WorksheetFunction.Match(wsED.Cells(x, empid), xRange, 0), _
WorksheetFunction.Match(wsED.Cells(empid, y), hdxRange, 0))
Next y
Next x
Application.ScreenUpdating = True
End Sub