When there are only two directly linked values, the Scripting.Dictionary
object can prove quite useful. The dictionary.Exists(key)
function makes it simple to test for valid keys. The similar Scripting.Collection
object lacks this functionality.
Here's a rough snippet showcasing how you might use it. Note that you'll need to set a reference to Microsoft Scripting Runtime or use late binding instead!
Sub Example()
Dim keyRange As Range
Dim valueRange As Range
Dim keyCell As Range
Dim valueCell As Range
Dim searchKey As String
Dim searchValue As String
Dim noDuplicateSearches As Boolean: noDuplicateSearches = True
Dim criteria As New Dictionary
With criteria ' criteria as key-value pairs
.Add "100", "Text 1"
.Add "200", "Text 2"
End With
Set keyRange = Sheets(1).Columns(1).SpecialCells(xlCellTypeConstants)
Set valueRange = Sheets(1).Columns(2).SpecialCells(xlCellTypeConstants)
For Each keyCell In keyRange
searchKey = keyCell.Value
If criteria.Exists(searchKey) Then ' on a valid key
searchValue = criteria(searchKey)
For Each valueCell In valueRange
If valueCell.Value = searchValue Then ' on a valid value
Debug.Print "Found", searchValue, _
"using", searchKey, _
"at", valueCell.Address
End If
Next valueCell
If noDuplicateSearches Then criteria.Remove searchKey
End If
Next keyCell
End Sub
Of course, generating the entire criteria
dictionary each time the sub is run is far from optimal. You could choose to use a different sub to initialize (or unload!) the dictionary in a public variable instead.