Discussion:
|
In our sample, the criteria is the minimum and maximum values that you would like the cells to have. You could use something like this to remove values that fall within a certain range, such as an age, where you only want to keep the ages between 4 and 15. Once selected, you can, for example, choose Edit-Delete and choose "entire row", or you could use additional code to copy the selected cells to another sheet.
To run the macro you will need to specify the range to work on, the minimum value, and the maximum value. The macro will then check every cell within your specified range and create a new range whose cells are within the minimum and maximum values specified.
In the example the range is A1:B10, Minimum Value is 4, Maximum Value is 15. You can change the range, minimum, and maximum values as needed.
|
Option Explicit
Sub SelectByValue(Rng1 As Range, MinimunValue As Double, MaximumValue As Double)
Dim MyRange As Range
Dim Cell As Object
For Each Cell In Rng1
If Cell.Value >= MinimunValue And Cell.Value <= MaximumValue Then
If MyRange Is Nothing Then
Set MyRange = Range(Cell.Address)
Else
Set MyRange = Union(MyRange, Range(Cell.Address))
End If
End If
Next
MyRange.Select
End Sub
Sub CallSelectByValue()
Call SelectByValue(Range("A1:B10"), 4, 15)
End Sub
|