Option Explicit
Private WithEvents cmbrs As CommandBars
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare PtrSafe Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare PtrSafe Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Private oHyperLinkCell As Object
Private oPrevSelection As Object
Private Sub Workbook_Activate()
Call StoreRecoverSubAddresses(True)
Set cmbrs = Application.CommandBars
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call StoreRecoverSubAddresses(True)
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Set cmbrs = Application.CommandBars
Set oPrevSelection = Target
End Sub
Sub StoreRecoverSubAddresses(ByVal Store As Boolean)
Dim oCell As Range
For Each oCell In ActiveSheet.UsedRange.Cells
If oCell.Hyperlinks.Count > 0 Then
If Store Then
oCell.Hyperlinks(1).Range.ID = oCell.Hyperlinks(1).SubAddress
Else
oCell.Hyperlinks(1).SubAddress = oCell.Hyperlinks(1).Range.ID
End If
End If
Next
End Sub
Private Sub cmbrs_OnUpdate()
Dim tCurPos As POINTAPI
Dim kbArray As KeyboardBytes
Application.CommandBars.FindControl(ID:=2040).Enabled = Not Application.CommandBars.FindControl(ID:=2040).Enabled
GetCursorPos tCurPos
Set oHyperLinkCell = ActiveWindow.RangeFromPoint(tCurPos.x, tCurPos.y)
If TypeName(oHyperLinkCell) = "Range" Then
GetKeyboardState kbArray
If oHyperLinkCell.Hyperlinks.Count Then
oHyperLinkCell.Hyperlinks(1).SubAddress = ""
If GetKeyState(vbKeyLButton) = 1 Then
If oPrevSelection.Address = ActiveWindow.RangeFromPoint(tCurPos.x, tCurPos.y).Address Then
Call StoreRecoverSubAddresses(False)
If MsgBox("Do you wish to navigate to the link?", vbYesNo) = vbNo Then
Else
oHyperLinkCell.Hyperlinks(1).Follow
End If
End If
End If
End If
End If
kbArray.kbByte(vbKeyLButton) = 0
SetKeyboardState kbArray
End Sub