vba to select cell with value equal to or less than textbox entry

ThomasOES

Board Regular
Joined
Aug 29, 2017
Messages
174
I have a column of data with a range named "SortCol". The data is sorted in ascending order. I can use Find to select a cell with a value I enter into a textbox. However, if the range("SortCol") doesn't have the value from the textbox then an error occurs. What code to select the nearest lower value cell if no value matches the textbox entry?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I have a column of data with a range named "SortCol". The data is sorted in ascending order. I can use Find to select a cell with a value I enter into a textbox. However, if the range("SortCol") doesn't have the value from the textbox then an error occurs. What code to select the nearest lower value cell if no value matches the textbox entry?

You can use Match Function.

Take a look:
https://docs.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.match

If match_type is 1, MATCH finds the largest value that is less than or equal to lookup_value. Lookup_array must be placed in ascending order: ...-2, -1, 0, 1, 2, ..., A-Z, FALSE, TRUE.
 
Upvote 0
Thanks for the tip Akuini

I've tried many combinations of Match Functions but not having any luck.
I need to select the cell in the Range("SortCol") that is equal or less than the TextBox2 entry

My latest attempt
Code:
Dim sortLoc As Range
Set sortLoc = WorksheetFunction.Match(Search_Form.TextBox2.Value, Range("SortCol"), 1)
Application.Goto sortLoc

Any help would be great

Tom
 
Upvote 0
Thanks for the tip Akuini

I've tried many combinations of Match Functions but not having any luck.
I need to select the cell in the Range("SortCol") that is equal or less than the TextBox2 entry

My latest attempt
Code:
Dim sortLoc As Range
Set sortLoc = WorksheetFunction.Match(Search_Form.TextBox2.Value, Range("SortCol"), 1)
Application.Goto sortLoc

Any help would be great

Tom

Match funtion returns the relative position of an item in an array that matches a specified value in a specified order.
So you need a long variable to get the result first then use that variable get the range, so:

Code:
Dim sortLoc As Range, n As Long
    n = WorksheetFunction.Match(Search_Form.TextBox2.Value, Range("SortCol"), 1)
    Set sortLoc = Range("SortCol").Cells(n)
    Application.Goto sortLoc
 
Upvote 0
Thanks again Akuini. The code works if I replace Search_Form.TextBox2.Value with a typed in value.

Like this

Code:
Dim sortLoc As Range, n As Long
n = WorksheetFunction.Match(0.05, Range("SortCol"), 1)
Set sortLoc = Range("SortCol").Cells(n)
Application.Goto sortLoc

Cant figure why the textbox value isn't read by code

Tom
 
Upvote 0
Hm, maybe you need to convert the textbox value to double, like this:

Code:
n = WorksheetFunction.Match(CDbl(Search_Form.TextBox2.Value), Range("SortCol"), 1)
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,632
Members
452,661
Latest member
Nonhle

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