Sub AdvancedVLookUpAnalysis() Dim wsSource As Worksheet Dim wsAnalysis As Worksheet Dim…
- User
Sub AdvancedVLookUpAnalysis() Dim wsSource As Worksheet Dim wsAnalysis As Worksheet Dim sourceTable As Range, targetTable As Range, col As Range Dim numRows As Long, numCols As Long, colIndex As Long Dim sourceSheetName As String, analysisSheetName As String Dim selectedHeaders As Collection Dim paramName As String Dim matchCol As Range Dim i As Long Dim matchCount As Long, notMatchCount As Long
On Error GoTo ErrorHandler ' Prompt user to select Source Table Set sourceTable = Application.InputBox("Select the Source Table for analysis", Type:=8) If sourceTable Is Nothing Then Exit Sub ' Set the source worksheet and sheet name Set wsSource = sourceTable.Worksheet sourceSheetName = wsSource.Name analysisSheetName = sourceSheetName & "_VLookUp" ' Prompt user to select Target Table Set targetTable = Application.InputBox("Select the Target Table for matching", Type:=8) If targetTable Is Nothing Then Exit Sub ' Ensure selected ranges are valid tables If sourceTable.Rows.Count < 2 Or targetTable.Rows.Count < 2 Then MsgBox "Both Source and Target tables must have headers and at least one row of data.", vbExclamation Exit Sub End If ' Prompt user to select columns to match Set selectedHeaders = New Collection For Each col In targetTable.Rows(1).Cells If col.Value <> "" Then If MsgBox("Do you want to match by column: " & col.Value & "?", vbYesNo) = vbYes Then selectedHeaders.Add col.Value End If End If Next col If selectedHeaders.Count = 0 Then MsgBox "No columns selected for matching.", vbExclamation Exit Sub End If ' Check if analysis sheet exists; if so, refresh it On Error Resume Next Set wsAnalysis = wsSource.Parent.Worksheets(analysisSheetName) On Error GoTo ErrorHandler If wsAnalysis Is Nothing Then ' If sheet does not exist, create it in the same workbook, next to the source sheet Set wsAnalysis = wsSource.Parent.Worksheets.Add(After:=wsSource) wsAnalysis.Name = analysisSheetName Else ' Clear existing data for refresh wsAnalysis.Cells.Clear End If ' Ensure the sheet is visible wsAnalysis.Visible = xlSheetVisible wsAnalysis.Activate ' Copy source table to analysis sheet sourceTable.Copy Destination:=wsAnalysis.Range("A1") ' Determine number of rows and columns in source table numRows = sourceTable.Rows.Count numCols = sourceTable.Columns.Count ' Add matched columns and perform VLOOKUPs for each selected header For i = 1 To selectedHeaders.Count paramName = selectedHeaders(i) ' Find the corresponding column in the source table colIndex = Application.Match(paramName, wsAnalysis.Rows(1), 0) If IsError(colIndex) Then MsgBox "Column '" & paramName & "' not found in Source Table.", vbExclamation Exit Sub End If ' Add a new column for VLOOKUP results wsAnalysis.Cells(1, numCols + i).Value = paramName & " VLook_UP" Set matchCol = wsAnalysis.Range(wsAnalysis.Cells(2, numCols + i), wsAnalysis.Cells(numRows, numCols + i)) ' Perform VLOOKUP matchCount = 0 notMatchCount = 0 For Each col In matchCol.Cells If Not IsError(Application.VLookup(wsAnalysis.Cells(col.Row, colIndex).Value, targetTable, 1, False)) Then col.Value = "Matched" matchCount = matchCount + 1 Else col.Value = "Not Matched" notMatchCount = notMatchCount + 1 End If Next col ' Generate summary table With wsAnalysis .Cells(1, numCols + selectedHeaders.Count + 2 + (i - 1) * 3).Value = "Result of " & paramName & " Matched" .Cells(2, numCols + selectedHeaders.Count + 2 + (i - 1) * 3).Value = "Matched" .Cells(3, numCols + selectedHeaders.Count + 2 + (i - 1) * 3).Value = "Not Matched" .Cells(4, numCols + selectedHeaders.Count + 2 + (i - 1) * 3).Value = "Total" .Cells(2, numCols + selectedHeaders.Count + 3 + (i - 1) * 3).Value = matchCount .Cells(3, numCols + selectedHeaders.Count + 3 + (i - 1) * 3).Value = notMatchCount .Cells(4, numCols + selectedHeaders.Count + 3 + (i - 1) * 3).Value = matchCount + notMatchCount End With Next i ' Confirm completion and reveal the sheet wsAnalysis.Visible = xlSheetVisible wsAnalysis.Select MsgBox "Analysis completed and results stored in sheet: " & analysisSheetName, vbInformation Exit Sub - Assistant
This VBA macro,
AdvancedVLookUpAnalysis, automates the task of analyzing data between two tables in Excel by using the VLOOKUP function. Here's a detailed breakdown of what this code accomplishes: - User
what else we can include in it ?
- Assistant
Enhancing the VBA macro can provide more functionality and flexibility. Here are some potential improvements and additional features you might consider implementing:
- User
- Assistant
The
AdvancedVLookUpAnalysisVBA macro is designed to automate the process of comparing data between two Excel tables using VLOOKUP. Here's what this code does step-by-step: - User
suggest improvement in this as well as you did in last without compromising anything in primary function of this code
- Assistant
- User
okay include all changes as suggested in the code
- Assistant
- User
- Assistant
- User
give me the complete code with inclusion of all suggestion
- Assistant
- User
now tell me in detail what this code will do step wise step in detail
- Assistant
- User
you said in earlier chat that xlookup is better than vlookup is it for this case ?
- Assistant
- User
then give me complete code with this modification
- Assistant
- User
cant we do the modification in automatically select the table on selecting or just clicking on sheet name in both the case
- Assistant
- User
no no this code is not giving option to move with among sheet and select range and neither it taking automatically can you do one change which is it should automatically take the table when i click on the sheet is it possible fisrt tell me this and how it will be done ?
- Assistant