by Allen Wyatt
(last updated June 30, 2018)
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).
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 Office 365.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...Discover More
You can create macros that run whenever Excel detects a certain event happening within an entire workbook. This tip ...Discover More
You can use macros to process information in your worksheets. You may want to use that macro to apply the italic ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.