|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I would like to automatically move a userform if it is covering the active cell in Excel. I know the coordinates and bounds of the userform relative to the Excel application window, however, I do not know how to access or calculate the active cell's position relative to the application window. I know the active cell's position within a pane, but to that I need to add offsets for row headers, column headers, pane dividers, and command bars.
Does anyone know how to do this? Is there a straightforward way to determine the position of the active cell within the application window or do you have to calculate it piecemeal? John |
|
#2
|
||||
|
||||
|
You can capture the current Mouse position when on the Worksheet_SelectionChange event and Store the XY coordinates in a variable.
First Add a new Module and copy and paste this code Code:
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Global mouseposition As POINTAPI
Here is some sample code that shows the XY values Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Call GetCursorPos(mouseposition) MsgBox "MOUSE X POSTION: " & mouseposition.X & vbCrLf & "MOUSE Y POSTION: " & mouseposition.Y End Sub Quote:
|
|
#3
|
|||
|
|||
|
Re: Cell Coordinates
Thanks for the reply. However, since the mouse position is independent of the active cell, I still need to know how to determine the active cell coordinates in pixels relative to the application window, so I can move the userform appropriately if it overlaps the activecell. Excel does this with the Edit>Find... dialog box. I just need to minimc that behavior.
John |
|
#4
|
||||
|
||||
|
I'm not sure I understand. If you declare two variables in the general declarations section of your worksheet
Public Xpos As Integer Public Ypos As Integer Then you can set these two variables when the cell changes. Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Call GetCursorPos(mouseposition) Xpos = mouseposition.X Ypos = mouseposition.Y End Sub That point is then stored. It is then irrelevant where the cursor goes after that until you change cells again. So if you click on a button to open your user form, you can then reference those two points and move your form around those two points when the form opens Code:
Private Sub UserForm_Activate() Me.Top = Sheet1.Ypos Me.Left = Sheet1.Xpos End Sub |
|
#5
|
|||
|
|||
|
Your approach will work if the selection change is made by the user clicking on a cell, but what about the case in which the new selection is made by range.select? The cursor position will not change in that case, but the active cell location will.
John |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Excel Macro - Cell coordinates for Window |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|