I have been trying to write some worthy piece of code but this keeps on failing, so was hoping on your expertise to help me out ;)
My table looks a little like the below:
And I need to fill in the blank with the following criteria:
If a field is empty, it needs to be filled with the value of Same country/Yellow/same year so Australia/green/2022 is empty so it needs to be filled by the value of Australia/Yellow/2022
If same country/yellow/same year is empty as well, then we take the value of the region/same color/same year as both australia/green/2022 and Australia/Yellow/2022 are empty, we need the value of Asia/green/2022
If that is empty as well, then we go Region/Yellow/same year so we would look for Asia/Yellow/2022 if the previous needed values are empty..
This is what I wrote for the first level (looking for same country/yellow/same year) but can't get the right thing to pinpoint the "results".
Sub Test()
Dim WB As Workbook
Dim WS As Worksheet
Set WB = ActiveWorkbook
Set WS = WB.Sheets("BASIS")
Dim LRow As Long, LCol As Long, r As Long, c As Long
LRow = WS.Range("B" & WS.Rows.Count).End(xlUp).Row
LCol = WS.Cells(LRow, 29)
Dim criteria1, criteria3, search1, search2, search3 As Range
Dim results As Range
Application.ScreenUpdating = False
For c = 8 To LCol
For r = 3 To LRow - 1
If WS.Cells(r, c) = "" Then
criteria1 = WS.Cells(r, 4)
criteria3 = WS.Cells(r, 6)
criteria4 = WS.Cells(2, c)
search1 = WS.Range("D3:D" & LRow)
search2 = WS.Range("E3:E" & LRow)
search3 = WS.Range("F3:F" & LRow)
search4 = WS.Range("H2:AC2")
results = WS.Range("H3:AC" & LRow)
WS.Cells(r, c).Value = Application.WorksheetFunction.XLookup(criteria1 & "Yellow" & criteria3 & criteria4, search1 & search2 & search3 & search4, results).Value
'ws.Cells(LRow, c).Value
End If
Next r
Next c
Application.ScreenUpdating = True
End Sub
Maybe one of you may help :)