lookup and select

JoboM

New Member
Joined
May 26, 2015
Messages
40
Does anyone know how to code vba to find a cell that has the same contents as a reference cell, say A1, and select that cell but offset by 1, ie. in the column to the left but same row??
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Code:
Set rCell = Range("C2:C50").Find(Range("A1").Value, LookIn:=xlValues)
rCell.Offset(0, -1).Select

That will find the first occurance of A1 in the range C2:C50, and select the cell to the left of the match.
 
Upvote 0
Thanks! Would this code go in the module section? Otherwise how exactly would you input the code?? Sorry, I am quite new to macros :(
 
Upvote 0
Depends when you want it to run. You could have a button that you click, or an on change event when A1 changes.
 
Upvote 0
This will run whenever A1 changes

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("A1"), Target) Is Nothing Then
        Set rcell = Range("C2:C50").Find(Range("A1").Value, LookIn:=xlValues)
        If Not rcell Is Nothing Then
            rcell.Offset(0, -1).Select
        Else
            MsgBox "No match found"
        End If
    End If
End Sub


Right click your sheet tab, select 'View Code', then paste the code in. Go back to worksheet and change A1 to test.
 
Upvote 0
Thank you! This works brilliantly.

Would it be possible to also code a command that returns to A1 after information is put into the cell that was previously selected??
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("A1"), Target) Is Nothing Then
        Set rcell = Range("C2:C50").Find(Range("A1").Value, LookIn:=xlValues)
        If Not rcell Is Nothing Then
            rcell.Offset(0, -1).Select
        Else
            MsgBox "No match found"
        End If
    End If
    If Not Intersect(Range("B2:B50"), Target) Is Nothing Then
        Range("A1").Select
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,145
Messages
6,053,750
Members
444,681
Latest member
Nadzri Hassan

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