Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function GetCursor Lib "user32" () As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetCursor Lib "user32" () As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Private WithEvents cmndbrs As CommandBars
Private Sub Workbook_Open()
Call HookCommandBars
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
If ActiveSheet Is Sheet1 And Not Sh Is Sheet1 Then Sh.Visible = xlSheetVeryHidden
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call HookCommandBars
End Sub
Private Sub HookCommandBars()
Set cmndbrs = Application.CommandBars
Call cmndbrs_OnUpdate
End Sub
Private Sub cmndbrs_OnUpdate()
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Static hCur As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Static hCur As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Static KeyState As Long
Dim tCurPos As POINTAPI
Dim oRangeUnderCursor As Object
Dim sTargetSheetName As String, sTargetRange As String
With Application.CommandBars.FindControl(ID:=2040)
.Enabled = Not .Enabled
End With
If GetForegroundWindow = FindWindow("wndclass_desked_gsk", vbNullString) Then
Set cmndbrs = Nothing: Exit Sub
End If
Call GetCursorPos(tCurPos)
On Error Resume Next
Set oRangeUnderCursor = ActiveWindow.RangeFromPoint(tCurPos.x, tCurPos.y)
If TypeName(oRangeUnderCursor) = "Range" Then
If InStr(oRangeUnderCursor.Formula, "=HYPERLINK") Then
sTargetSheetName = Split(oRangeUnderCursor.Formula, "&")(2)
sTargetRange = Split(Split(oRangeUnderCursor.Formula, "&")(4), ",")(0)
If Sheets(Evaluate(sTargetSheetName).Text).Visible <> xlSheetVisible Then
If KeyState <> GetKeyState(VBA.vbKeyLButton) Then
If GetCursor <> hCur Then
Application.OnTime Now, "'" & Me.CodeName & ".Follow_Hyperlink """ & _
sTargetSheetName & """,""" & sTargetRange & "'"
End If
End If
End If
Else
hCur = GetCursor
End If
End If
KeyState = GetKeyState(VBA.vbKeyLButton)
End Sub
Private Sub Follow_Hyperlink(ByVal ShName As String, ByVal RngAddr As String)
Dim oSheet As Worksheet
Dim GetCutCopyRange As Range
Set GetCutCopyRange = CallByName(Sheets(Evaluate(ShName).Text), "Range", VbGet, Range(RngAddr))
Sheets(Evaluate(ShName).Text).Visible = xlSheetVisible
Application.Goto GetCutCopyRange, True
End Sub