Creating a Floating Macro Button

Written by Allen Wyatt (last updated June 12, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


11

Leah would like a floating button with a macro assigned. So, as she moves left, right, up, or down, the button stays near the cell on which she is working. Leah wonders if there is a way to add such a button in Excel.

There is not a way to do this without using a macro to control the placement of the button. (This shouldn't be a big problem as you are already using at least one macro in the workbook—the one that the floating button triggers.)

Before getting into the specifics of how to do this, you'll want to consider if you really want to use this approach. For instance, you could assign your macro to a shortcut key or to a button on the Quick Access Toolbar. Either approach would remove the need to constantly reposition your floating button.

If you definitely want to go the button route, then go ahead and create your on-screen button and link it to your macro. Then, simply create an event handler that repositions the button every time you change the selected cell. For instance, let's assume that the name of your button is "Button 1". In that case, you could use the following macro:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With ActiveSheet.Shapes("Button 1")
        .Top = Target.Offset(1).Top
        .Left = Target.Offset(, 1).Left
    End With
End Sub

Remember that this is an event handler, so it needs to be placed in the code window for the worksheet to which it applies. It results in the button being moved to always be near the bottom-right corner of the selected cell.

It should also be noted that there are two ways you can create a macro button—either as an Active X control or as a legacy, non-Active X control. The above approach works great if you are using the legacy type of button. If, however, you are using an Active X control, then you'll want to change the macro just a bit:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With ActiveSheet.OLEObjects("CommandButton1")
        .Top = Target.Offset(1).Top
        .Left = Target.Offset(, 1).Left
    End With
End Sub

Note that the only change is in the object being referred to. With the appropriate event handler in place, whenever you change the cell selected on the screen, the button repositions to be just to the bottom-right of the selected cell. Thus, it isn't a true "floating" button that hovers in the same spot all the time. This should be OK in Leah's situation, since she wanted the button to be near the cell in which she is working (the selected cell), not the cell she is viewing (such as when she scrolls doing using the scroll bars).

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 (13544) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

Spacing After Sentences

Word can check to see if you have a consistent number of spaces at the end of your sentences.

Discover More

Using a Macro to Change the Formatting of All Instances of a Word

If you have a word that you need to make sure is formatted the same way throughout your document, there are several ways ...

Discover More

Creating a List of Files in a Directory

Do you need a list of all the files in a directory? It's easy to create if you use the proper command-line commands.

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Pulling Cell Names into VBA

Excel allows you to define names that can refer either to ranges of cells or to constant information, such as formulas. ...

Discover More

Displaying Worksheets in a Slideshow Fashion

Want to step through the worksheets in a workbook, displaying them like a slideshow? The macros provided in this tip can ...

Discover More

Reversing Names In Place

Do you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...

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 8 - 5?

2020-06-18 10:01:51

J. Woolley

@Matias
To clarify my last comment, you should NOT use BOTH Worksheet_SelectionChange and Workbook_SheetSelectionChange event handlers. The latter is NOT recommended for this Tip because buttons are usually Sheet dependent.


2020-06-18 08:05:33

Barry

The code as shown has an issue when the selected cell is near the lower edge or right-hand edge of the visible sheet as the button will no longer be visible. The following code overcomes this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet.Shapes("Button 1")
If Target.Offset(1).Top > Application.Height - ActiveWindow.Height Then
.Top = Target.Top - .Height
Else
.Top = Target.Offset(1).Top
End If
If Target.Offset(0, 1).Left > Application.Width - .Width Then
.Left = Target.Left - .Width
Else
.Left = Target.Offset(0, 1).Left
End If
End With
End Sub

Another way is to have a button on a modeless Userform then there is no need have a macro move the button every time the selection changes with issues over multiple cell selection and if the selected cells is in the bottom right of the worksheet.

The Userform1 will have a Title bar and Close button. The Close button can be disabled quite easily, but the removal of the title bar is a bit more complex ( see https://www.teachexcel.com/excel-tutorial/2026/removing-the-title-bar-from-a-userform)


2020-06-17 10:33:23

J. Woolley

@Matias
When you open a workbook and press Alt+F11 you will see the Visual Basic Editor (VBE). If your workbook has several worksheets like Sheet1, Sheet2, etc., you should see these listed under Project - VBAProject > Microsoft Excel Objects. You should also see the ThisWorkbook object listed after the Sheet objects.

The original Worksheet_SelectionChange event handler code described in the Tip should be in each of the Sheet objects that has a button. The Workbook_SheetSelectionChange event handler described in my previous comment should be in the ThisWorkbook object. In the latter case, parameter Sh identifies which Sheet object had a selection change. In either case, the name of the button Shape or OLEObject will probably be different for each Sheet (Sh).


2020-06-17 03:00:11

Matias

@J.Woolley
Perhaps another dum question:
I implemented the changes you suggested and everything works throughout the whole workbook
but I would like to use it only on the sheet I have the actual button, not any other sheets.

Dont understand where to adjust that?


2020-06-16 11:18:15

J. Woolley

@MARCO GOUVEIA
To fix the bug, change the two lines related to Target as follows:
.Top = Target.Cells(1).Offset(1).Top
.Left = Target.Cells(1).Offset(, 1).Left
To apply to the whole workbook, change the first line as follows:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
then substitute Sh in place of ActiveSheet.


2020-06-15 05:31:47

MARCO GOUVEIA

Another q... How do you suggest fixing the bug that happens when more than one line is selected?


2020-06-15 04:34:04

MARCO GOUVEIA

Hi! Thank you for the macro! Would it be easy to make the macro work in the whole workbook?


2019-01-28 07:35:26

Ken Varley

Navin: I think you may have mis-typed the question....i don't know what you are asking

I still have my test, but never did anything with it.

It runs without a problem, and I have tried inputting to different cells without a problem

Sorry i can't help


2019-01-22 15:49:32

Navin Lam

Runs fine but when I click the any of the row number, error pops-up:

"Run-Time Error '1004':
Application-defined or object-defined error. "

Any workaround? Thanks.


2018-07-04 04:18:42

Ken Varley

UPDATE ON PREVIOUS COMMENT:

Closed workbook after it didn't work. Re-opened workbook next day, and it was working.

Must be the MS fairy godmother looking over me !!!


2018-07-02 06:07:49

Ken Varley

Didn't work for me

I thought that I would give it a try. Inserted an activeX button (CommandButton1).
Copied code into worksheet.

Tried making entries in different cells

Nothing happens


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.