how to capture a mouse click event in an Excel Spreadsheet.

I have some code below that will open an excel spreadsheet from a vb application. How can I capture the cell click event on the excel spreadsheet sheet ?

I want the user to click on a cell and then have that cell range captured and then I can do something with it.

 Public VariableExcel1 As Boolean = False
    Private Column As Integer = 1
    Private Row As Integer = 1
    Private ReadingROW As Integer = 1
    Private ReadingColumn As Integer = 1
    Private HeaderAlreadyTaken As Boolean = False
    Private OpenExisting_ExcelTemplate As Boolean = False
    Public xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Public xlWorkBook As Excel.Workbook
    Public xlWorkSheet As Excel.Worksheet
    Public obj As Object
    Public rng As Excel.Range
    Public ExistingExcelFile As String = ""

 Private Sub ToolbarExcel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolbarExcel.Click
        Dim TargetAction As String = "FormSeach - Excel Button Pressed."
            If _masterGlobalParameters.masterGlobal.masterUserLogging = True Then
            End If
            SerialPortStatus.Text = ""
            If xlApp Is Nothing Then
                MessageBox.Show("The App could not recognize a support Excel Program please make sure a supported program is installed.")
            End If
            VariableExcel1 = False
            If VariableExcel1 = False Then
                tmrLiveReading.Enabled = False
                serialCOMDiscover.Write("<#C" & OriginaBaseRFChannel)
                obj = CreateObject("WScript.Shell")
                obj.appactivate("Microsoft Excel")
                obj = Nothing
                xlApp.Visible = True
                _ExcelExport = True
                '  xlWorkBook.add()
                xlWorkBook = xlApp.Workbooks.Add()
                xlWorkSheet = xlWorkBook.Sheets("sheet1")
                Column = 1
                Row = 1
                HeaderAlreadyTaken = False
            End If
        Catch ex As Exception
            If _masterGlobalParameters.masterGlobal.masterErrorWarning = True Then
            End If
        End Try
    End Sub

Open in new window

Who is Participating?
Unfortunately Excel sheet has not click event. You can use
   xlWorkSheet = xlWorkBook.Sheets(1) 'I suggest using index instead of name
        AddHandler xlWorkSheet.SelectionChange, AddressOf Sheet_SelectionChanged
        AddHandler xlWorkSheet.BeforeDoubleClick, AddressOf Sheet_BeforeDoubleClick
        AddHandler xlWorkSheet.BeforeRightClick, AddressOf Sheet_BeforeRightClick
    Private Sub Sheet_SelectionChanged(ByVal Target As Microsoft.Office.Interop.Excel.Range)
        Debug.Print("Selection changed to " & Target.Address)
    End Sub
    Private Sub Sheet_BeforeDoubleClick(ByVal Target As Microsoft.Office.Interop.Excel.Range, ByRef cancel As Boolean)
        Debug.Print("Before double click on " & Target.Address)
    End Sub
    Private Sub Sheet_BeforeRightClick(ByVal Target As Microsoft.Office.Interop.Excel.Range, ByRef cancel As Boolean)
        Debug.Print("Before right click on " & Target.Address)
    End Sub

Open in new window


Maybe something like this
Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _
    Handles DisplayAddress.Click

    Dim smartTagAddress As String = e.Range.Address( _
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub

Open in new window

cmdolcetAuthor Commented:

I am a little unfamiliar with SmartTags  Is that an add in that I need? Also I am programming in vb 2005.

This line is coming up as an error (  ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _
    Handles DisplayAddress.Click)

The type Microsoft office tools is not defined? and the DisplayAddress requires a with event.
sorry can't help further
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.