thk12205
03-29-2018, 09:08 AM
Excel Version: 2013 Professional Plus
I am trying to make a cell change values only if it is selected by a left click.
I tried to fix it by adding in "if left clicked" macro I found online that allows left clicks to be tracked by VBA.
Unfortunately, Public Declare Function is not allowed in the Microsoft Excel Objects, and changing it to Private seems to make the code not run at all (after I click, nothing activates, not even error pop up)
How can I fix this? And is there better way of getting macros to activate only with left click on cell?
Public Declare Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Long) As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
For n = 7 To 25
'Set thisCell = Range(Cells(n, "H"))
If Target.Address = Cells(n, "H").Address Then
If GetAsyncKeyState(vbKeyLButton) And &H8000 Then
If Cells(n, "H") = 0 Then
If Cells(n, "I") = 3 Then
Cells(n, "H") = 1
Range(Cells(n, "F"), Cells(n, "G")).Select
Exit Sub
ElseIf Cells(n, "I") = 2 Then
MsgBox "System is 'In-Work'"
ElseIf Cells(n, "I") = 1 Then
MsgBox "System is 'Line Stopped'"
End If
ElseIf Cells(n, "H") = 1 Then
Cells(n, "H") = 0
Range(Cells(n, "F"), Cells(n, "G")).Select
Exit Sub
Else
Cells(n, "H") = 0
Range(Cells(n, "F"), Cells(n, "G")).Select
Exit Sub
End If
End If
End If
Next
Application.ScreenUpdating = True
End Sub
I am trying to make a cell change values only if it is selected by a left click.
I tried to fix it by adding in "if left clicked" macro I found online that allows left clicks to be tracked by VBA.
Unfortunately, Public Declare Function is not allowed in the Microsoft Excel Objects, and changing it to Private seems to make the code not run at all (after I click, nothing activates, not even error pop up)
How can I fix this? And is there better way of getting macros to activate only with left click on cell?
Public Declare Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Long) As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
For n = 7 To 25
'Set thisCell = Range(Cells(n, "H"))
If Target.Address = Cells(n, "H").Address Then
If GetAsyncKeyState(vbKeyLButton) And &H8000 Then
If Cells(n, "H") = 0 Then
If Cells(n, "I") = 3 Then
Cells(n, "H") = 1
Range(Cells(n, "F"), Cells(n, "G")).Select
Exit Sub
ElseIf Cells(n, "I") = 2 Then
MsgBox "System is 'In-Work'"
ElseIf Cells(n, "I") = 1 Then
MsgBox "System is 'Line Stopped'"
End If
ElseIf Cells(n, "H") = 1 Then
Cells(n, "H") = 0
Range(Cells(n, "F"), Cells(n, "G")).Select
Exit Sub
Else
Cells(n, "H") = 0
Range(Cells(n, "F"), Cells(n, "G")).Select
Exit Sub
End If
End If
End If
Next
Application.ScreenUpdating = True
End Sub