How to find the first value in a range that is greater than x

harshdua

New Member
Joined
Jan 7, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I currently have a macro set up to find values in a range, however some values dont exist in the column and as such i end up getting an error 91. To get around this, i want it to find the next range. My current macro is as follows.

VBA Code:
Sub xfg()
Dim findnextGL As Range
Dim nextGL As Integer
nextGL = 51
Do Until nextGL = 1001
Set findnextGL = Range("B02:B4899").Find(nextGL, LookIn:=xlValues)
If findnextGL Is Nothing Then

Debug.Print error 91

Exit Sub
Else
Debug.Print findnextGL.Address
nextGL = nextGL + 50

End If
Loop
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You need to move this line outside of the loop
VBA Code:
nextGL = nextGL + 50
and get rid of the Exit Sub as Dave suggested.
 
Upvote 0
You need to move this line outside of the loop
VBA Code:
nextGL = nextGL + 50
and get rid of the Exit Sub as Dave suggested.
Sorry i worded my question poorly

I want the macro to find the first value that is greater than nextGL within the range of findnextGL if findnextGL is nothing
 
Upvote 0
In that case replace Exit Sub with
VBA Code:
nextGL = nextGL + 1
assuming your values are whole numbers
 
Upvote 0
In that case replace Exit Sub with
VBA Code:
nextGL = nextGL + 1
assuming your values are whole numbers
This however changes the current value of nextGL and disrupts the search i have set up

I need to find the addresses of the cells that are equal to every number that satisfies the following expression, (51+50n)
ie: i need it to find the cell equal to (51,101,151,201...)
in case it doesnt exist, i need it to find the first value that is greater than nextGL within the range. For example, if my column is of the following, within the first loop, since 51 does not exist, findnextGL is set to equal to the cell of 54, and it continues and finds me the cells of 101,151,201...
50
54
67
59
101
151
201
 
Upvote 0
How about
VBA Code:
Sub xfg()
   Dim findnextGL As Range
   Dim nextGL As Long, GLinc As Long
   nextGL = 51
   Do Until nextGL = 1001
      Set findnextGL = Range("B2:B4899").Find(nextGL, LookIn:=xlValues)
      If findnextGL Is Nothing Then
         GLinc = nextGL
         Do Until GLinc = nextGL + 49
            GLinc = GLinc + 1
            Set findnextGL = Range("B2:B4899").Find(GLinc, LookIn:=xlValues)
            If Not findnextGL Is Nothing Then
               Debug.Print findnextGL.Address, GLinc
               Exit Do
            End If
         Loop
      Else
         Debug.Print findnextGL.Address
      End If
      nextGL = nextGL + 50
   Loop
End Sub
 
Upvote 0
How about
VBA Code:
Sub xfg()
   Dim findnextGL As Range
   Dim nextGL As Long, GLinc As Long
   nextGL = 51
   Do Until nextGL = 1001
      Set findnextGL = Range("B2:B4899").Find(nextGL, LookIn:=xlValues)
      If findnextGL Is Nothing Then
         GLinc = nextGL
         Do Until GLinc = nextGL + 49
            GLinc = GLinc + 1
            Set findnextGL = Range("B2:B4899").Find(GLinc, LookIn:=xlValues)
            If Not findnextGL Is Nothing Then
               Debug.Print findnextGL.Address, GLinc
               Exit Do
            End If
         Loop
      Else
         Debug.Print findnextGL.Address
      End If
      nextGL = nextGL + 50
   Loop
End Sub
I Love you man <3 no ****
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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