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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.