Skip to content

Instantly share code, notes, and snippets.

@aalfson
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalfson/fff28926946e1ed227e2 to your computer and use it in GitHub Desktop.
Save aalfson/fff28926946e1ed227e2 to your computer and use it in GitHub Desktop.
Select All Cells that Match a Selected Cell - Excel VBA Macro
'Excel macro that will select all of the cells that match the value contained in the currently selected cell.
Sub SelectAllThatMatch()
Set sourceCell = ActiveCell
Set selectedCells = sourceCell
Dim rwIndex As Integer
Dim rwMax As Integer
rwMax = ActiveSheet.UsedRange.Rows.Count
Dim colIndex As Integer
Dim colMax As Integer
colMax = ActiveSheet.UsedRange.Columns.Count
For rwIndex = 1 To rwMax
For colIndex = 1 To colMax
If Cells(rwIndex, colIndex).Value = sourceCell.Value Then
Set selectedCells = Application.Union(selectedCells, Cells(rwIndex, colIndex))
End If
Next colIndex
Next rwIndex
selectedCells.Select
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment