This is just a macro that scans the UsedRange of the worksheet for other ranges with a set of values that are identical to those in the selected range. When such a range is found a MsgBox shows its address. The search for other such ranges continues after OK is clicked. If no duplicate ranges are found a MsgBox states "No other range on this sheet has that set of values.
Public Sub find_range()
Dim vaLookFor As Variant
Dim vaLookAt As Variant
Dim stLookForAddress As String
Dim iRowCounter1 As Long
Dim iRowCounter2 As Long
Dim iColumnCounter1 As Integer
Dim iColumnCounter2 As Integer
Dim stResult As String
Dim FoundCount As Long
stResult = "Looking"
stLookForAddress = Application.InputBox(prompt:="Select the range of cells to look for", Default:=Selection.Address, Type:=8).Address
vaLookFor = Range(stLookForAddress)
vaLookAt = ActiveSheet.UsedRange
'Move across one column
For iColumnCounter1 = 1 To UBound(vaLookAt, 2) - UBound(vaLookFor, 2) + 1
'Move down one row
For iRowCounter1 = 1 To UBound(vaLookAt, 1) - UBound(vaLookFor, 1) + 1
If iRowCounter1 = 1 Then Let stResult = "Looking"
'Check values in columns
For iColumnCounter2 = 1 To UBound(vaLookFor, 2)
'Check values in rows
For iRowCounter2 = 1 To UBound(vaLookFor, 1)
'Exit For Next loop checking rows as soon as row values not equal
If vaLookAt(iRowCounter1 + iRowCounter2 - 1, iColumnCounter1 + iColumnCounter2 - 1) _
<> vaLookFor(iRowCounter2, iColumnCounter2) Then
Let stResult = "Not Equal"
Exit For
End If
Next iRowCounter2
'Exit For Next loop checking columns because an unequal cell has been found
'Change stResult to "Looking" so that next part of Used Area is checked
If stResult = "Not Equal" Then
Let stResult = "Looking"
Exit For
ElseIf iColumnCounter2 = UBound(vaLookFor, 2) Then 'All cells equal, now tell user the address
If Range(Cells(iRowCounter1, iColumnCounter1), _
Cells(iRowCounter1 + UBound(vaLookFor, 1) - 1, iColumnCounter1 + UBound(vaLookFor, 2) - 1)).Address <> Selection.Address Then
FoundCount = FoundCount + 1
MsgBox Range(Cells(iRowCounter1, iColumnCounter1), _
Cells(iRowCounter1 + UBound(vaLookFor, 1) - 1, iColumnCounter1 + UBound(vaLookFor, 2) - 1)).Address
End If
End If
Next iColumnCounter2
Next iRowCounter1
Next iColumnCounter1
If FoundCount = 0 Then MsgBox "No other range on this sheet has that set of values"
End Sub
Ken Johnson
|