View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default Identify adjacency cells with same value

Paul D Smith wrote:

Is there a simple way to identify all the adjacency cells (in a row or
column) with the same value? I can obviously do this with a loop but I'm
hoping there is a VBA function that might do this more quickly.


How about this?

Sub matches()
Dim found As Collection
Set found = New Collection
v = ActiveCell.Value
If v = "" Then Exit Sub
For Each cell In Rows(ActiveCell.Row).Cells
If cell.Column ActiveCell.SpecialCells(xlCellTypeLastCell).Column _
Then Exit For
If cell.Value < "" Then
If cell.Column < ActiveCell.Column Then
If v = cell.Value Then
found.Add cell
End If
End If
End If
Next
For Each cell In Columns(ActiveCell.Column).Cells
If cell.Row ActiveCell.SpecialCells(xlCellTypeLastCell).Row Then _
Exit For
If cell.Value < "" Then
If cell.Row < ActiveCell.Row Then
If v = cell.Value Then
found.Add cell
End If
End If
End If
Next
'do what you need here, then...
Set found = Nothing
End Sub

At the end of the sub, you have a collection called "found" that contains
each cell that matches ActiveCell.Value. You can use that to do whatever you
need -- for example, if you just need a list of addresses:

For n = 1 To found.Count
Debug.Print found.Item(n).Address
Next

Perhaps a bit over-complicated, but it works...

--
Chainsaws are not generally accepted as an IT equipment must have.