Macro to confine cursor to set boundaries

mackensteff

Board Regular
Joined
Feb 9, 2010
Messages
74
Office Version
  1. 365
Platform
  1. MacOS
Hello,

Is it possible by vba to restrict the mouse movements to a given area of the screen. Ideally, I would like the cursor to be confined to a large button area once the button is pressed and triggers a macro, but by pressing esc or a similar key regain free movement, but then be confined to the button area once it is pressed again. Is this remotely possible (or does that even make sense)?

Thanks!!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello,

Is it possible by vba to restrict the mouse movements to a given area of the screen. Ideally, I would like the cursor to be confined to a large button area once the button is pressed and triggers a macro, but by pressing esc or a similar key regain free movement, but then be confined to the button area once it is pressed again. Is this remotely possible (or does that even make sense)?

Thanks!!

Could be done with a few API calls ? Is the button on a Userform or on a worksheet ?
 
Upvote 0
Would this work for you.
Code:
Sub ToggleBoundary()
    With ActiveSheet
        If .ScrollArea = "" Then
            .ScrollArea = "A1:F10"
        Else
            .ScrollArea = ""
        End If
    End With
End Sub
 
Upvote 0
Hello,

Jaafar the button is on a worksheet. mikerickson Thanks for the code. I would have mentioned I am using a Mac. I tried the code and it didn't do anything. Looking into it I saw others had issues with ScrollArea support in Excel for Macs. The other, and most likely option, is I don't know what I'm doing.
 
Upvote 0
Hello,

Jaafar the button is on a worksheet. mikerickson Thanks for the code. I would have mentioned I am using a Mac. I tried the code and it didn't do anything. Looking into it I saw others had issues with ScrollArea support in Excel for Macs. The other, and most likely option, is I don't know what I'm doing.

If it wasn't a Mac, I could have posted some code.
 
Upvote 0
I'm on a Mac too.
The code I posted worked for me. Attach it to a forms menus command button. Press the button once, and you can only scroll in A1:F10, press again, you can scroll anywhere. Note that F11 will be visible both when scrolling is restricted and when not.
 
Last edited:
Upvote 0
I don't know what I am doing wrong then. I did as you said last night and tried again just now and nothing happens when I press the button. Sorry for the trouble.
 
Upvote 0
Yeah, he might be. Super dictator app. If my mouse was suddenly restricted, if I couldn't click on quitl, it would scare the willies out of me.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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