7 Replies Latest reply on Apr 9, 2012 11:18 AM by Jagandeep Singh

    How to get and set cursor position in Microsoft Excel using VBA

    Jagandeep Singh

      I am working on a VBA Macro in Solidworks which require a way to get and set current cursor position in Microsoft Excel Workbook. Kindly guide me how to do it ?

       

      Regards

      Jagandeep

          • Re: How to get and set cursor position in Microsoft Excel using VBA
            Jagandeep Singh

            Thanks Michael but this is not what I want. This is to set/get the mouse pointer position but I simply want the position of "Rectangular cell highlighter" or whatever you call it in terms of (1,2) ,(5,7) etc to enter the text in different cells.

              • Re: How to get and set cursor position in Microsoft Excel using VBA
                Rajat Jain

                Jagandeep,

                 

                The attached macro should do what you intend. You need to activate the module and run it.

                 

                The macro records the X & Y mouse co-ordinates of the mouse in active view of the active Document. You can set a value and click set to move mouse pointer.

                 

                This was made in SW 2012 so if your version is any different then you need to change references.

                 

                Hope it helps!!!

                 

                Rajat Jain

                  • Re: How to get and set cursor position in Microsoft Excel using VBA
                    Jagandeep Singh

                    Dear Rajat,

                    This is not what i want. I want to get/set current cell position in microsoft excel so as to enter text at various cells wrt to current cell position.

                      • Re: How to get and set cursor position in Microsoft Excel using VBA
                        Michael Jeffries

                        Jagandeep,

                         

                        If I am reading you correctly, you simply want to enter data into cells in the background while Solidworks is running.  Given you already know which cells you want to enter data into, this should be fairly simple. Let me know if the following code helps!  I assume you have already done so, but please make sure you add the excel 12.0 object type reference to your program!

                         

                        Capture.PNG

                         

                        ***************************************************************

                         

                        Dim swApp As Object

                        Dim xlApp As Excel.Application

                        Dim xlbook As Excel.Workbook

                        Dim xlsheet As Excel.Worksheet

                         

                         

                        Sub main()

                         

                         

                        Set xlApp = CreateObject("excel.application")   'Sets Excel Application Object

                        Set xlbook = Excel.Workbooks.Add("")            'Creates a New Workbook

                        'Set xlbook = Excel.Workbooks.Open("C:\temp\testworkbook.xls", , False) 'Sets the Workbook to an Existing One, the True/false sets the Workbook to Read Only

                        Set xlsheet = xlbook.Worksheets("Sheet2")       'Set Sheet by Tab Name

                        'Set xlsheet = xlbook.Worksheets(2)              'Set Sheet by Index Number (starting at 1)

                            With xlsheet    'Edit the Worksheet Tab Selected above

                                .Cells(1, 1).Value = "Test" 'Edits Column A, Row 1

                                .Cells(2, 5).Value = "B5"   'Edits Column B, Row 5

                            End With

                         

                         

                            xlApp.Visible = True     'Makes the Excel Workbook Visible

                           

                            'xlbook.PrintOut     'Prints the Workbook

                            'xlbook.Close (True) 'Closes the Workbook and saves changes

                            'xlApp.Quit      'Closes the current instance of Excel referenced above, this should NOT close instances of excel you did not open with the code

                           

                           

                        Set swApp = Application.SldWorks

                        End Sub

                        • Re: How to get and set cursor position in Microsoft Excel using VBA
                          Dan Miel

                          Jagandeep,

                          I believe this might be what you are looking for. You must have the Excel reference in tools selected. The program below should print out the row, column and value or the active worksheet to the immediate window. I normally have just one excel open because if there is more than one instance of Excel open this may grab the wrong Excel.

                           

                          'Written 04-09-2012 Dan Miel

                           

                          Sub main()

                          Dim xlApp As New Excel.Application

                          Set xlApp = GetObject(, "Excel.Application")

                          Debug.Print "Y = " & xlApp.ActiveCell.Row

                          Debug.Print "X = " & xlApp.ActiveCell.Column

                          Debug.Print "V = " & xlApp.ActiveCell.Value

                          End Sub