Go to cell in other sheet based on value in double clicked cell

DataPanda

New Member
Joined
Jun 22, 2022
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2011
  6. 2010
  7. 2007
Platform
  1. Windows
  2. MacOS
Hi.

I'm trying to write some VBA in a rush but haven't had time to properly learn it to struggling a bit

What I want, is that when someone clicks or double clicks on a cell in Column B Sheet 1 (e.g. folder B), then the VBA will look at the adjacent Column A Sheet 1 (which will be hidden) and find the value there (e.g. C:/folderA/folderB). It will search Column A Sheet 2 for this value and (a) set that as the active cell and (b) set it to be the top-left cell on the screen.

I've identified some VBA that may help, I just don't know how to put it all together:

VBA Code:
' Set cell as the top left cell
Application.Goto <something here>, True

` Change active cell to cell on the left
ActiveCell.Offset(0, -1)

Sheet 1:

FOLDERS (column A Sheet 1)CONTENTS (column B Sheet 1)
C:/folderAfolderA
C:/folderA/folderB folderB
C:/folderCfolderC
C:folderDfolderD

Sheet 2:


PATHS (column A Sheet 2)
C:/folderA
C:/folderA/folderB
C:/folderA/folderB/fileA.txt
C:/folderC
C:/folderC/fileB.txt
C:folderD
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Sheet1
Book8 (version 1).xlsb
AB
1FoldersContents
2C:/folderAfolderA
3C:/folderA/folderBfolderB
4C:/folderCfolderC
5C:folderDfolderD
6C:/folderXfolderX
Sheet1


Sheet2
Book8 (version 1).xlsb
A
1Paths
2C:/folderA
3C:/folderA/folderB
4C:/folderA/folderB/fileA.txt
5C:/folderC
6C:/folderC/fileB.txt
7C:folderD
Sheet2


Code must be located in the Sheet1 code module.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim SearchTerm As String
    Dim WS As Worksheet
    Dim rngFound As Range

    If Not Application.Intersect(Target, Me.Columns("B")) Is Nothing And Target.Cells.Count = 1 Then
        SearchTerm = Target.Offset(0, -1).Value
        If Trim(SearchTerm) <> "" Then
            Set WS = ThisWorkbook.Worksheets("Sheet2")
            With WS
                Set rngFound = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Find(What:=SearchTerm, LookAt:=xlWhole)
            End With
            If Not rngFound Is Nothing Then
                WS.Activate
                rngFound.Select
            Else
                MsgBox "'" & SearchTerm & "' not found"
            End If
        End If
    End If
End Sub
 
Upvote 0
Sheet1
Book8 (version 1).xlsb
AB
1FoldersContents
2C:/folderAfolderA
3C:/folderA/folderBfolderB
4C:/folderCfolderC
5C:folderDfolderD
6C:/folderXfolderX
Sheet1


Sheet2
Book8 (version 1).xlsb
A
1Paths
2C:/folderA
3C:/folderA/folderB
4C:/folderA/folderB/fileA.txt
5C:/folderC
6C:/folderC/fileB.txt
7C:folderD
Sheet2


Code must be located in the Sheet1 code module.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim SearchTerm As String
    Dim WS As Worksheet
    Dim rngFound As Range

    If Not Application.Intersect(Target, Me.Columns("B")) Is Nothing And Target.Cells.Count = 1 Then
        SearchTerm = Target.Offset(0, -1).Value
        If Trim(SearchTerm) <> "" Then
            Set WS = ThisWorkbook.Worksheets("Sheet2")
            With WS
                Set rngFound = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Find(What:=SearchTerm, LookAt:=xlWhole)
            End With
            If Not rngFound Is Nothing Then
                WS.Activate
                rngFound.Select
            Else
                MsgBox "'" & SearchTerm & "' not found"
            End If
        End If
    End If
End Sub
Thank you!

I did get something working in the end. But will try this next week when I'm in the office again and see how it compares.
 
Upvote 0
I would go for the double-click event so that the code is not firing so much of the time. This code includes this requirement:
and (b) set it to be the top-left cell on the screen.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim rFound As Range
  
  If Not Intersect(Target, Columns("B")) Is Nothing Then
    Cancel = True
    Set rFound = Sheets("Sheet2").Columns("A").Find(What:=Target.Offset(, -1).Value, LookAt:=xlWhole)
    If rFound Is Nothing Then
      MsgBox Target.Offset(, -1).Value & " not found"
    Else
      Application.Goto Reference:=rFound, Scroll:=True
    End If
  End If
End Sub

If you would prefer to use the selection change event ..

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rFound As Range
  
  If Not Intersect(ActiveCell, Columns("B")) Is Nothing Then
    Set rFound = Sheets("Sheet2").Columns("A").Find(What:=ActiveCell.Offset(, -1).Value, LookAt:=xlWhole)
    If rFound Is Nothing Then
      MsgBox ActiveCell.Offset(, -1).Value & " not found"
    Else
      Application.Goto Reference:=rFound, Scroll:=True
    End If
  End If
End Sub
 
Upvote 0
If you would prefer to use the selection change event ..

The problem with using If Not Intersect(ActiveCell, Columns("B")) Is Nothing Then instead of
If Not Application.Intersect(Target, Me.Columns("B")) Is Nothing And Target.Cells.Count = 1 Then for the selection event is that any operation that selects more than one cell (such as deciding to copy data from col B to somewhere else) will result in a runtime error.

Edit: No runtime error on your code, I see. But it still jumps the user away from the selected cells.
 
Upvote 0
I would go for the double-click event so that the code is not firing so much of the time. This code includes this requirement:


VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim rFound As Range
 
  If Not Intersect(Target, Columns("B")) Is Nothing Then
    Cancel = True
    Set rFound = Sheets("Sheet2").Columns("A").Find(What:=Target.Offset(, -1).Value, LookAt:=xlWhole)
    If rFound Is Nothing Then
      MsgBox Target.Offset(, -1).Value & " not found"
    Else
      Application.Goto Reference:=rFound, Scroll:=True
    End If
  End If
End Sub

If you would prefer to use the selection change event ..

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rFound As Range
 
  If Not Intersect(ActiveCell, Columns("B")) Is Nothing Then
    Set rFound = Sheets("Sheet2").Columns("A").Find(What:=ActiveCell.Offset(, -1).Value, LookAt:=xlWhole)
    If rFound Is Nothing Then
      MsgBox ActiveCell.Offset(, -1).Value & " not found"
    Else
      Application.Goto Reference:=rFound, Scroll:=True
    End If
  End If
End Sub
Thank you. I had tried your first bit of code, as the `BeforeDoubleClick` event I think is what I want. However I did seem to get an out of range issue for the line that starts `Set`. I'll look into more closely when I'm next online.
 
Upvote 0
However I did seem to get an out of range issue for the line that starts `Set`.
That would most likely be because the sheet name in the code does not match a sheet name in your workbook.
Rich (BB code):
Set rFound = Sheets("Sheet2").Columns("A").Find(What:=Target.Offset(, -1).Value, LookAt:=xlWhole)

(such as deciding to copy data from col B to somewhere else) ...

.... But it still jumps the user away from the selected cells.
The issues does not seem to be relevant to the OP but in any case that would be the same issue with your code if you wanted to copy a single cell from col B to somewhere else. :)
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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