Need Help Matching a variable and defining as a range

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
I'd like to have a variable. find it in column of data, and then define the value that is Offset(0,1) as a range.

i thought i had it, but couldnt quite get the code right. I need this for VBA.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Something like this?

Code:
Sub test()
Dim x As Variant, Found As Range
x = "This is the chosen row"
Set Found = Columns("F").Find(what:=x, LookIn:=xlValues, lookat:=xlWhole).Offset(1)
If Not Found Is Nothing Then MsgBox Found.Address
End Sub
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
thank you VoG,
quick question, i know i didnt state this, but what if the value isn't exact, is there a way to find the closest value. sorry.
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200

ADVERTISEMENT

numbers. probably off by some decimals
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Maybe something like this (change the rounding criteria to suit)

Code:
Sub test()
Dim x As Variant, Found As Range, LR As Long, i As Long
x = 1.234
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("A" & i)
        If Round(.Value, 0) = Round(x, 0) Then
            Set Found = .Offset(1)
            Exit For
        End If
    End With
Next i
If Not Found Is Nothing Then MsgBox Found.Address
End Sub
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200

ADVERTISEMENT

is x where you put how much you want to allow rounding?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
No, here

Rich (BB code):
Round(.Value, 0) = Round(x, 0) Then

0 rounds to 0 decimal places. If you wanted to round to 2 decimal places use 2.
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
so, .value is the value in the column we found(so we dont change decimal)

and x is the number we want it to be, up to how ever many decimal points( and if the number is
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
x is the number being searched for. The code rounds both x and the value in column A to the same number of decimal places and tests for equality.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,732
Messages
5,833,380
Members
430,206
Latest member
Sami Gaid

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
Top