Making a Macro Button Stay Put

Written by Allen Wyatt (last updated November 21, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


2

Nitin created a button in a worksheet and linked a macro to the button. The button works fine; when he clicks it, the macro runs. However, when he scrolls up or down in the worksheet, the button moves. Nitin wonders if there is a way to make the button stay where it is so that it doesn't move.

This is a variation of a question addressed in a previous ExcelTip. In that tip, entitled "Creating a Floating Macro Button," the result was a button that would be positioned relative to whatever cell was selected in the worksheet. In Nitin's situation, he wants one that is positioned relative to the window on the screen, not to any cell in the worksheet.

There are a few ways you can approach this issue. The first may be the simplest—add your macro to a button on the Quick Access Toolbar or, if you prefer, to a ribbon customization. Either approach is fast and easy, and both the QAT and ribbon will always appear at the same place in the Excel window.

If you want to use the button-within-the-worksheet approach, however, then one thing you can try is to change the properties of the button. If you inserted the button as an ActiveX control (from the Developer tab of the ribbon), you can right-click on the button and choose Format Control from the resulting Context menu. This displays the Format Control dialog box, and you should make sure the Properties tab is displayed. (See Figure 1.)

Figure 1. The Properties tab of the Format Control dialog box.

You want to select the third option on the dialog box, "Don't move or size with cells." Theoretically this will stop your button from moving around on the screen.

I say "theoretically" because I've had mixed results with this approach—sometimes it will work and sometimes it won't. If it works for you, that's great; you've solved your issue. If it doesn't work for you, then trying to track down why is (in my experience) an exercise in futility—there is no rhyme or reason as to why it won't work.

If that is your experience, then another approach is to anchor the button to the top row of your worksheet and then freeze the position of the top row. (How you freeze a row is covered in the ExcelTips named "Making a Named Range Non-Scrollable" and "Freezing Both Rows and Columns.")

If you prefer a macro approach, then you can develop a short event handler that can do the trick for you. To use the event handler, display the code window for the worksheet you want affected (right-click the worksheet tab and choose View Code) and then place the following into the code window:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo 0
    With ActiveSheet.Shapes("Button 1")
        .Top = ActiveWindow.VisibleRange.Top + 5
        .Left = ActiveWindow.VisibleRange.Left + 5
    End With
End Sub

This code works with an ActiveX control which, again, is created by using the tools on the Developer tab of the ribbon. If you are using a legacy, non-ActiveX control, then you'll need to change the With line in the macro:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo 0
    With ActiveSheet.OLEObjects("CommandButton1")
        .Top = ActiveWindow.VisibleRange.Top + 5
        .Left = ActiveWindow.VisibleRange.Left + 5
    End With
End Sub

In fact, you can change the With line to reflect whatever type of object you are using for your button. For instance, here is the version that will work if your button is a regular shape of some sort:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo 0
    With ActiveSheet.Shapes("Rectangle 1")
        .Top = ActiveWindow.VisibleRange.Top + 5
        .Left = ActiveWindow.VisibleRange.Left + 5
    End With
End Sub

In this case it is important to make sure that the With line reflects whatever name you've assigned to the shape you are using as your button.

Once you get the With addressing down correctly for your situation, you'll note that as you move through the worksheet by pressing the navigation keys (Up, Down, Left, Right, PgUp, and PgDown), the button will stay in the same location relative to the Excel window. If you use the scroll bars or the mouse to move up, down, left, or right, then the button will scroll off the screen. It will, however snap back into view once you click on a cell.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13801) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of the Startup Spreadsheet

When you start Excel, you normally see a blank worksheet displayed. Here's how to get rid of it.

Discover More

Engraving and Embossing Text

Word allows you to format your text in a number of different ways. Two rather esoteric ways to format your text are by ...

Discover More

Jumping to the End of the Document in Error

If you inadvertently move to the end of the document, you might be wondering how to get your insertion point back to ...

Discover More

Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!

More ExcelTips (ribbon)

Renaming a Macro

Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.

Discover More

Determining the Current Directory

When you use a macro to do file operations, it works (by default) within the current directory. If you want to know which ...

Discover More

Unprotecting Groups of Worksheets

Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight minus 5?

2020-11-23 10:10:08

David Bonin

I've sometimes had problems with macro buttons not staying put even with "Don't move or size with cells".

After arguing with Excel for a while, and learning its more stubborn than me, I finally wrote a macro that puts all buttons back in their place when the worksheet is activated. If the buttons were already in place, no harm is done. If not, they're fixed.


2020-11-21 05:37:50

Chris

Thanks, Allen. "Don't move or size with cells." didn't work for me, as you said might happen, but freezing the top two rows and placing the button there worked just fine.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.