Go to cell in a column that matches the value of another cell

kivikatz

New Member
Joined
Sep 12, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all. I would like to add VBA code that scrolls to cell in a column which matches the value of another cell. For instance if a cell in Column A1:A100 matches cell B1, I would like to scroll down to the cell in Column A. Alternatively, is it possible if I manually click on cell B1 that it will scroll down to the matching cell in Column A.

I've searched around but cannot seem to find a solution. Thanks for all your help.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
VBA Code:
Sub Macro1()

' Find the last row number in column A.
    Dim lastRow As Long
    With Sheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

' Find the value you are looking for
    lookupvalue = Worksheets("Sheet1").Range("B1").Value
 
' Select the range you are looking in
    lookuprange = Worksheets("Sheet1").Range("A1:A" & lastRow)
 
' Find the first matching value, and return the row number
' If there is no matching number, the macro will jump to "ErrorMessageBox"
    On Error GoTo ErrorMesageBox
    FirstMatchRowNumber = WorksheetFunction.Match(lookupvalue, lookuprange, 0)

' Go to the applicable row.
    Worksheets("Sheet1").Range("A" & FirstMatchRowNumber).Select
Exit Sub



' In case of an unknown number, the macro will show an message box
ErrorMesageBox:
    MsgBox "The number entered in B1 is not known in A1:A" & lastRow
    Exit Sub
End Sub
 
Upvote 0
Another option
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Fnd As Range
   
   If Target.Count > 1 Then Exit Sub
   If Target.Address(0, 0) = "B1" And Target.Value <> "" Then
      Set Fnd = Range("A:A").Find(Target.Value, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Activate
      Else
         MsgBox Target.Value & " not found"
      End If
   End If
End Sub
This needs to go in the relevant sheet module.
 
Upvote 0
Hi All. Sorry for the late reply. I was tied up with things and did not have a chance to check the post. It seems I am missing something with both of the solutions. Maybe it's because the data in column A and cell B2 is calculated based on input from another sheet. Could this be the problem why the solutions doesn't work, or am I doing something wrong with adding the VBA code?

Is it possible to click on cell B2 and then scroll to the matching cell in column A. Thanks!
 
Upvote 0
Do you mean B1 as in your op, orB2?
 
Upvote 0
Ok, how about a double click event
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Dim Fnd As Range
  
   If Target.Address(0, 0) = "B1" And Target.Value <> "" Then
      Set Fnd = Range("A:A").Find(Target.Value, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Activate
      Else
         MsgBox Target.Value & " not found"
      End If
   End If
End Sub
 
Upvote 0
Ok, how about a double click event
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Dim Fnd As Range
 
   If Target.Address(0, 0) = "B1" And Target.Value <> "" Then
      Set Fnd = Range("A:A").Find(Target.Value, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Activate
      Else
         MsgBox Target.Value & " not found"
      End If
   End If
End Sub
Thanks for trying to help me out, I tried the solution, but it doesn't work for me. Not sure what I am missing. When I double click on B1, I get a message box "Not Found". even though the data in B1 exists in one of the cells in column A.
 
Upvote 0
Is B1 an exact match or a partial match? Also what sort of values do you have?
 
Upvote 0

Forum statistics

Threads
1,215,495
Messages
6,125,149
Members
449,208
Latest member
emmac

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