VBA: move userform so that active cell is visible

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:

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.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,213,543
Messages
6,114,240
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top