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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,714
Messages
6,121,048
Members
449,007
Latest member
cloamp

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