Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

VBA Excel: Perform index match dynamically based on a config Sheet v2

Hello experts,

I have this excellent VBA code which allows me to perform index match actions through a config Sheet.

Option Explicit

Sub RunIndexMatch_revised()
    Dim wsConfig As Worksheet, wsResult As Worksheet, wsTargetSheet As Worksheet
    Dim sSheetName As String, sTargetColumn As String, sCompareColumn As String, sMatchColumn As String, sDestinationColumn, sSheetName2 As String
    Dim rgTarget As Range, rgCompare  As Range, rgMatch As Range, c As Range
    Dim rw As Integer, MaxRowTargetSheet As Long
    Dim vMatchRow As Variant
    
    CheckConfigSheet
    
    Set wsConfig = Worksheets("Config")
    Set wsResult = Worksheets("Result")
    Application.ScreenUpdating = False
    For rw = 2 To wsConfig.Range("A1").CurrentRegion.Rows.Count
        sSheetName = wsConfig.Range("A" & rw)
        Set wsTargetSheet = Worksheets(sSheetName)
        sSheetName2 = wsConfig.Range("F" & rw)
        Set wsResult = Worksheets(sSheetName2)
        MaxRowTargetSheet = wsTargetSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
        sTargetColumn = wsConfig.Range("B" & rw)
        sCompareColumn = wsConfig.Range("C" & rw)
        sMatchColumn = wsConfig.Range("D" & rw)
        sDestinationColumn = wsConfig.Range("E" & rw)
        wsTargetSheet.Select
        Set rgTarget = wsTargetSheet.Range(sTargetColumn & "2:" & sTargetColumn & MaxRowTargetSheet)
        Set rgMatch = wsTargetSheet.Range(sMatchColumn & "2:" & sMatchColumn & MaxRowTargetSheet)
        wsResult.Select
        Set rgCompare = wsResult.Range(sCompareColumn & "2", Range(sCompareColumn & Rows.Count).End(xlUp))
        wsResult.Range(sDestinationColumn & "2:" & sDestinationColumn & (rgCompare.Rows.Count + 1)).ClearContents
        For Each c In rgCompare
            vMatchRow = Application.Match(c, rgMatch, 0)
            If IsNumeric(vMatchRow) Then
                wsResult.Range(sDestinationColumn & c.Row) = Application.WorksheetFunction.Index(rgTarget, vMatchRow, 1)
            End If
        Next c
    Next rw
End Sub

Sub CheckConfigSheet()
    Dim wsConfig As Worksheet, ws As Worksheet, rw As Integer, col As Integer, i As Integer, WarningText As String
    Set wsConfig = Worksheets("Config")
    For rw = 2 To wsConfig.Range("A1").CurrentRegion.Rows.Count
        i = 0
        For Each ws In Worksheets
            If wsConfig.Range("A" & rw) <> "" Then
                If UCase(ws.Name) = UCase(wsConfig.Range("A" & rw)) Then
                    i = i + 1
                End If
                If UCase(ws.Name) = UCase(wsConfig.Range("F" & rw)) Then
                    i = i + 1
                End If
            End If
        Next ws
        For col = 2 To 5
            If wsConfig.Cells(rw, col) <> "" And Len(wsConfig.Cells(rw, col)) <= 2 Then
                If WorksheetFunction.IsText(wsConfig.Cells(rw, col)) Then
                    i = i + 1
                End If
            End If
        Next col
        If i <> 6 Then
            WarningText = "Warning" & Chr(10) & "Data entered in Config Sheet row " & CStr(rw) & " is not consistent, please check that:"
            WarningText = WarningText & Chr(10) & "1-Target/Comparedvalue and Destination  Sheets exist or there is a misspelled mistake or you haven't entered data."
            WarningText = WarningText & Chr(10) & "Required columns entered in Range(B:E) are alphabetical and not numeric."
            WarningText = WarningText & Chr(10) & Chr(10) & "Program stop"
            MsgBox WarningText, vbCritical
            End
        End If
    Next rw
End Sub

Open in new window



I would like to add two additional requirements based on values reported in column G and H.

User generated image
1.If value reported in column G is equal to 0 perform the index match without any change. If flag is equal to 1 perform just the index match for the destination values which doesn't have information reported empty values, this means that the index match should be skipped for the values already filled in Destination column.
 It would be great to add a verification loop if the value is not equal to 1 or 0

 2.Report the count in column H related to the indexmatch values performed in Destination column.


Thank you very much for your help.
Dynamic-index-match-3.xlsm
SOLUTION
Avatar of Ejgil Hedegaard
Ejgil Hedegaard
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank  you for this code however I have a problem concerning the flag equal 0. Probably I didn't specified properly.
User generated imageWhen I enter the flag = 0 in my Config Sheet and I enter manually test data in Destination Column Result-2 the macro didn't replace those values and it should replace them and don't skip them so the count is wrong.
User generated image
18 values hasn't been replaced as the 3 values I entered manually before calling the macro
I think that the best is adapt the macro with the following:
If flag = 0 reset destination  range  and then perform the index match as when flag = 0 we don't want to omit anything we launch the index match without any condition.

I test the flag=1 and it works properly as it perform the index match just for the destination empty values.

Thank you very much for your help.
I was able to adapt based on my need by modifying the flag condition. Let me know if you think there is another better way to do this.
Dynamic-index-match-4.xlsm
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works, thank you again. Excellent explanation!