Method or VBA script to select any cells below a certain value in a selection of data

justin_n84

Board Regular
Joined
Jan 29, 2017
Messages
59
Hi,

I'm looking for a method or VBA script to select any cells below a certain value (for example 5.1) in a selection of data.

Any help would be greatly appreciated.

Thanks in advance
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
Dim cel As Range, rng As Range
For Each cel In Selection
    If cel < 5.1 Then
        If rng Is Nothing Then
            Set rng = cel
        Else
            Set rng = Union(rng, cel)
        End If
    End If
Next
rng.Select
 
Upvote 0
Hope this helps

Code:
Option Explicit


Dim LastRowNo As Long
Dim Rloop As Long
Dim RangeStr As String


Sub MinValRangeSelect()
LastRowNo = Range("A65536").End(xlUp).Row 'Just to set list of names


If LastRowNo > 1 Then
    RangeStr = ""
    For Rloop = 1 To LastRowNo
        If Range("A" & Rloop).Value < 5.1 Then
            RangeStr = RangeStr & "A" & Range("A" & Rloop).Row & ","
        End If
    Next Rloop
    If Len(RangeStr) > 0 Then
        RangeStr = Left(RangeStr, Len(RangeStr) - 1)
    End If
    Range(RangeStr).Select
End If
End Sub
 
Upvote 0
Amended :
Code:
Sub FT()
Dim cel As Range, rng As Range[COLOR=#ff0000]
For Each cel In Intersect(Selection, ActiveSheet.UsedRange)[/COLOR]
    If cel < 5.1 Then
        If rng Is Nothing Then
            Set rng = cel
        Else
            Set rng = Union(rng, cel)
        End If
    End If
Next
[COLOR=#ff0000]If Not rng Is Nothing Then rng.Select[/COLOR]
End Sub
 
Last edited:
Upvote 0
If all of the cells being selected are constants (that is, there are no formulas in the cells), then this completely different method than what has been posted to date will also do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub SelectLessThan5point1WithinSelection()
  Dim LessThanValue As Double, Addr As String, Data As Variant
  LessThanValue = [B][COLOR="#FF0000"]5.1[/COLOR][/B]
  On Error GoTo NoValues
  Intersect(Selection, ActiveSheet.UsedRange).Select
  Data = Selection.Value
  Addr = Selection.Address
  Selection = Evaluate(Replace("IF(@="""","""",SUBSTITUTE(@-" & LessThanValue & ",""-"",""=""))", "@", Selection.Address))
  Selection.SpecialCells(xlFormulas).Select
  Range(Addr) = Data
NoValues:
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
If all of the cells being selected are constants (that is, there are no formulas in the cells), then this completely different method than what has been posted to date will also do what you want...
Code:
[/FONT][TABLE="width: 500"]
<tbody>[TR]
[TD]Sub SelectLessThan5point1WithinSelection()
  Dim LessThanValue As Double, Addr As String, Data As Variant
  LessThanValue = [B][COLOR=#FF0000]5.1[/COLOR][/B]
  On Error GoTo NoValues
  Intersect(Selection, ActiveSheet.UsedRange).Select
  Data = Selection.Value
  Addr = Selection.Address
  Selection = Evaluate(Replace("IF(@="""","""",SUBSTITUTE(@-" & LessThanValue & ",""-"",""=""))", "@", Selection.Address))
  Selection.SpecialCells(xlFormulas).Select
  Range(Addr) = Data
NoValues:
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
[FONT=Verdana]


Needs amendment to accommodate when there are no values less than 5.1
 
Last edited:
Upvote 0


Needs amendment to accommodate when there are no values less than 5.1

Code:
Sub SelectLessThan5point1WithinSelection()
Dim LessThanValue As Double, Addr As String, Data As Variant
LessThanValue = 5.1
On Error GoTo NoValues
Intersect(Selection, ActiveSheet.UsedRange).Select
Data = Selection.Value
Addr = Selection.Address
Selection = Evaluate(Replace("IF(@="""","""",SUBSTITUTE(@-" & LessThanValue & ",""-"",""=""))", "@", Selection.Address))
Selection.SpecialCells(xlFormulas).Select
[COLOR=#ff0000]NoValues:[/COLOR]
[COLOR=#ff0000]Range(Addr) = Data[/COLOR]
End Sub
 
Upvote 0


Needs amendment to accommodate when there are no values less than 5.1
Good catch!



Code:
Sub SelectLessThan5point1WithinSelection()
Dim LessThanValue As Double, Addr As String, Data As Variant
LessThanValue = 5.1
On Error GoTo NoValues
Intersect(Selection, ActiveSheet.UsedRange).Select
Data = Selection.Value
Addr = Selection.Address
Selection = Evaluate(Replace("IF(@="""","""",SUBSTITUTE(@-" & LessThanValue & ",""-"",""=""))", "@", Selection.Address))
Selection.SpecialCells(xlFormulas).Select
[COLOR=#ff0000]NoValues:[/COLOR]
[COLOR=#ff0000]Range(Addr) = Data[/COLOR]
End Sub
I never tested the code for none of the cells meeting the condition (stupid of me not to:oops:). What you posted above is the correct fix. Thank you for that.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top