excel_monkey
New Member
- Joined
- Feb 17, 2009
- Messages
- 12
I want to move my userform automatically if it covers the active cell.
So far I have managed to use the .top property to have a first attempt.
The problem with this is that .top returns a value relative to the "container" of the object. I.e. the userform is contained by the application (I think), wheras the active window is contained in the space below the toolbars. This means that the .top value for the active cell and the .top value for the userform aren't really comparable.
Here is my code:
The problem is that form.top measures the distance from the top of the application whereas selection.top measures from the top of the active window.
I guess I need some way to find out the height of the tool bars or something, but I don't know.
Any suggestions / help much appreciated. I'm sure there must be a way to do this.
So far I have managed to use the .top property to have a first attempt.
The problem with this is that .top returns a value relative to the "container" of the object. I.e. the userform is contained by the application (I think), wheras the active window is contained in the space below the toolbars. This means that the .top value for the active cell and the .top value for the userform aren't really comparable.
Here is my code:
Code:
top_win = ActiveWindow.VisibleRange.Top
left_win = ActiveWindow.VisibleRange.Left
top_sel = Selection.Top - top_win
bot_sel = Selection.Top + Selection.Height - top_win
left_sel = Selection.Left - left_win
right_sel = Selection.Left + Selection.Width - left_win
top_form = form1.Top
bot_form = form1.Top + preced_trace.Height
left_form = form1.Left
right_form = form1.Left + preced_trace.Width
'***move to the right if blocking active cell***'
If (left_sel < right_form And right_sel > left_form ) _
And (top_sel < bot_form And bot_sel > top_form) Then 'selection is at least partially obscured
form1.left = form1.left + 100
End If
The problem is that form.top measures the distance from the top of the application whereas selection.top measures from the top of the active window.
I guess I need some way to find out the height of the tool bars or something, but I don't know.
Any suggestions / help much appreciated. I'm sure there must be a way to do this.