Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 9th, 2005, 11:41 PM
JohnClark JohnClark is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 3 JohnClark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 18 sec
Reputation Power: 0
Post Excel Macro - Cell coordinates for Window

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

Reply With Quote
  #2  
Old June 10th, 2005, 10:54 AM
blicci's Avatar
blicci blicci is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Philly
Posts: 351 blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 5 sec
Reputation Power: 52
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:
Originally Posted by JohnClark
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

Reply With Quote
  #3  
Old June 10th, 2005, 11:17 AM
JohnClark JohnClark is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 3 JohnClark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 18 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old June 10th, 2005, 11:53 AM
blicci's Avatar
blicci blicci is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Philly
Posts: 351 blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 5 sec
Reputation Power: 52
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

Reply With Quote
  #5  
Old June 10th, 2005, 05:56 PM
JohnClark JohnClark is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 3 JohnClark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 18 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Excel Macro - Cell coordinates for Window


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway