-5

I need help in capturing the cell value on left mouse click. So when a user has done a single left mouse click in a particular cell then I need to capture that particular cell value (Value written in that cell) in the VBA code.

This value will then be passed on to the VBA code and the output will be different for click in different cells. I hope I was able to explain the purpose.

I have total of 10 cells where the left mouse click value is to be captured.

4
  • I can think of SelectionChange events, though this would be if, e.g., B1 was selected then you clicked on B2, but not B1 and reselect B1. Commented Sep 13, 2017 at 16:48
  • In thinking, you could have part of your code to reset the selection to, say, A1, then no matter the click it can copy, if in the range. Commented Sep 13, 2017 at 16:51
  • 1
    Not so many are willing to write a code for you from scratch. You should come here with a half full glass, instead of an empty one, so that we can explain you why your glass is empty and advise what to do next. Commented Sep 13, 2017 at 16:54
  • 1
    Try declaring some windows DLL and use it. e.g.: Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer or Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer Commented Sep 13, 2017 at 17:02

3 Answers 3

2

This code will check to see if multiple cells are selected at once then it checks to see if the cell is empty. If it is empty then it exits otherwise it stores the value is cell N1. You can change which cell the value gets stores at. If N1 has a value it goes to the next empty cell in column N.

Dim oval
Dim N As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count < 2 Then
    If Target = Empty Then
      Exit Sub
    End If
oval = Target.Value
MsgBox "The value saved is " + oval + "."
If Range("N1").Value = "" Then
    N = 1
Else
    N = Cells(Rows.Count, "N").End(xlUp).Row + 1
End If
Cells(N, "N").Value = oval
End If
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Mr DaBeau96.With whatever limited knowledge of VBA I have your answer seems to me the nearest one.
Thanks Mr DaBeau96. With my limited knowledge of VBA, your answer seems to me the nearest one. Before asking the question in the forum I did extensive research on Google and understood that there is no left mouse click event capture in VBA, so any solution will be a workaround only. However VBA has double click mouse event capture functionality so the answer posted by Mr K Davis is also correct. So I did my homework and it was not that I came here with an empty glass as mentioned by Mr Tehscript. I hope this forum helps the amateur users like me as well. Thanks to all who provided the solution
I knew there wasn't a mouse click option and that the closest thing to it was SelectionChange in my experience. I wasn't sure how you wanted the values stored so I just put them in a cell so you could easily see which values you wanted stored
1

If you would settle for a double mouse click, you could try something like

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    MsgBox Target.Value
    Cancel = True
End Sub

Comments

0

How about this? Just wrote it, it will not register a mouse click if you click where you could have reached with the keyboard

Enter Under Worksheet Code:

XXXXXXX
Option Explicit
Private prevTarget As Range
Private Sub Worksheet_Activate()
Set prevTarget = Selection
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim wasMouseClick As Boolean
    Dim ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9 As String
    On Error Resume Next
    ch1 = prevTarget.Offset(1, 0).Address
    ch2 = prevTarget.Offset(-1, 0).Address
    ch3 = prevTarget.Offset(0, 1).Address
    ch4 = prevTarget.Offset(0, -1).Address
    ch5 = prevTarget.End(xlDown).Address
    ch6 = prevTarget.End(xlToLeft).Address
    ch7 = prevTarget.End(xlToRight).Address
    ch8 = prevTarget.End(xlUp).Address
    On Error GoTo error_noPreTarget

    If Not (Target.Address = ch1 Or _
       Target.Address = ch2 Or _
       Target.Address = ch3 Or _
       Target.Address = ch4 Or _
       Target.Address = ch5 Or _
       Target.Address = ch6 Or _
       Target.Address = ch7 Or _
       Target.Address = ch8) Then
        wasMouseClick = True
    End If
    Set prevTarget = ActiveCell
    If wasMouseClick Then
        Debug.Print wasMouseClick ' replace with what you want when wasMouseClick = True
    End If

Exit Sub
error_noPreTarget:
Set prevTarget = Selection
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.