Double Click Cell to cell to Sheet Name and Cell

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have sheet names in Col G2 onward followed by the cell number eg BRB85, SouthC91 etc. BR is the sheet name and B85 is cell number, South is sheet name and C91 is the ell number

I have tried to write code, so that when I double click any cell with Data in Col G from row2 onward, it will take be to the sheet name and dell number

I get message sheet not found, but Sheet does Exist

Kindly amend my code

Code:
 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim cellValue As String
    Dim sheetName As String
    Dim cellNumber As String
    Dim i As Integer
    Dim ws As Worksheet

    ' Check if double-clicked cell is in column G and not blank
    If Target.Column = 7 And Target.Value <> "" Then
        cellValue = Target.Value

        ' Extract sheet name and cell number
        For i = 1 To Len(cellValue)
            If Not IsNumeric(Mid(cellValue, i, 1)) Then
                sheetName = Left(cellValue, i - 1)
                cellNumber = Mid(cellValue, i)
                Exit For
            End If
        Next i

        ' Try to activate the sheet directly
        On Error Resume Next
        Set ws = Sheets(Left(sheetName, WorksheetFunction.Search("B", sheetName) - 1))
        On Error GoTo 0

        ' Check if sheet activation was successful
        If Not ws Is Nothing Then
            ' Check if cell number is valid
            If IsNumeric(cellNumber) Then
                ' Go to the specified sheet and cell
                ws.Activate
                ws.Range(cellNumber).Select
            Else
                ' Invalid cell number
                MsgBox "Error: Invalid cell number!"
            End If
        Else
            ' Sheet not found
            MsgBox "Error: Sheet not found!"
        End If

        ' Cancel the default double-click action
        Cancel = True
    End If
End Sub
 
Okay, just added some more error checking on cell row numbers:
VBA Code:
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim cellValue As String
    Dim sheetName As String
    Dim cellNumber As String
    Dim i     As Integer
    Dim ws    As Worksheet
    'check if double-clicked cell is in column G and not blank
    If Target.Column = 7 And Target.Value <> "" Then
        cellValue = Target.Value
        'extract sheet name and cell number
        For i = 1 To Len(cellValue)
            If IsNumeric(Mid(cellValue, i, 1)) Then
                On Error Resume Next
                sheetName = Left(cellValue, i - 2)
                cellNumber = Mid(cellValue, i - 1)
                On Error GoTo 0
                Exit For
            End If
        Next i
        Debug.Print sheetName & " " & cellNumber
        'try to activate the sheet directly
        On Error Resume Next
        Set ws = Sheets(sheetName)
        On Error GoTo 0
        'check if sheet activation was successful
        If Not ws Is Nothing Then
            'check if cell number is valid
            If Mid(cellNumber, 2) <= Rows.Count Then
                'go to the specified sheet and cell
                ws.Activate
                ws.Range(cellNumber).Select
            Else
                'invalid cell number
                MsgBox "Error: Invalid cell number !"
            End If
        Else
            'sheet not found
            MsgBox "Error: Sheet not found !"
        End If
        'cancel the default double-click action
        Cancel = True
    End If
End Sub
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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