Microsoft Office Tutorials and References
In Depth Information
Example 19-10. The SelectIfDifferent Procedure
currently equal to Nothing , in which case the Union function will (unfortunately) return
Nothing . In other words:
Application.Union(Something, Nothing) = Nothing
Example 19-10. The SelectIfDifferent Procedure
Private Sub SelectIfDifferent()
Dim rngMatch As Range
Dim vCellValue As Variant
Dim vPreviousCellValue As Variant
Dim cMatches As Integer
Dim oCell As Object
Dim cRows As Integer, cColumns As Integer
Dim r As Integer, c As Integer
' Get row and column count (one of which is 1)
cColumns = rngSearch.Columns.Count
cRows = rngSearch.Rows.Count
' Start search
cMatches = 0
Set rngMatch = Nothing
For r = 1 To cRows
For c = 1 To cColumns
Set oCell = rngSearch.Cells(r, c)
vCellValue = oCell.Value
vCellValue = CStr(vCellValue)
If r = 1 And c = 1 Then
' Include first cell
If rngMatch Is Nothing Then
Set rngMatch = oCell
Else
Set rngMatch = Application.Union(rngMatch, oCell)
End If
cMatches = cMatches + 1
' Save value for next comparison
vPreviousCellValue = vCellValue
Else
' Do comparison with previous cell
vCellValue = rngSearch.Cells(r, c).Value
vCellValue = CStr(vCellValue)
If vCellValue <> vPreviousCellValue Then
If rngMatch Is Nothing Then
Set rngMatch = oCell
Else
Set rngMatch = Application.Union(rngMatch, oCell)
End If
cMatches = cMatches + 1
End If
' Save value for next comparion
vPreviousCellValue = vCellValue
End If
Next ' column
 
Search JabSto ::




Custom Search