-
-
Save Nirelko/f8aacfaee3236b3a3eac5391862ac129 to your computer and use it in GitHub Desktop.
VBA code for finding an item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub FindMatchedValues() | |
Dim sheetName As String | |
Dim startIndex As Integer | |
Dim itemsAmount As Integer | |
Dim targetValue As Integer | |
Dim indicatorLetter As String | |
Dim valuesLetter As String | |
Dim values As Range | |
Dim matchedValuesLetter As String | |
Dim matchedValuesStartIndex As String | |
Dim matchedValues As New Collection | |
sheetName = "Sheet1" | |
startIndex = 2 | |
itemsAmount = 4 | |
targetValue = 1 | |
indicatorLetter = "B" | |
valuesLetter = "A" | |
Set values = Worksheets(sheetName).Range(valuesLetter & startIndex & ":" & valuesLetter & (itemsAmount + startIndex - 1)) | |
matchedValuesLetter = "C" | |
matchedValuesStartIndex = 1 | |
For Each indicator In Worksheets(sheetName).Range(indicatorLetter & startIndex & ":" & indicatorLetter & (itemsAmount + startIndex - 1)) | |
If indicator.Value = targetValue Then | |
matchedValues.Add values(indicator.Row - startIndex + 1).Value | |
End If | |
Next | |
For matchedValueIndex = 1 To matchedValues.Count | |
Worksheets(sheetName).Range(matchedValuesLetter & (matchedValuesStartIndex + matchedValueIndex)).Value = matchedValues.Item(matchedValueIndex) | |
Next | |
End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment