How to Have a Floating Command Button on a Worksheet in Excel?

Let us assume we have an Active X command on an Excel sheet at one place, and the data in the Excel sheet is in large volumes. If you want to use the active x command, we need to search for it every time. This problem can be solved by making the button floating. This article will help you understand how we can make the button always float in Excel.

Floating a Command Button on a Worksheet

Here we will first insert a button and then assign a macro to the button to make it always floating. Let us see a simple process for always floating a command button on a worksheet. We can find the Active X commands under the developer option in Excel.

Step 1

Let us consider an Excel sheet where an active X command button is present, as shown in the below image. To insert an active X command, click on developer, click on insert, and select any command.

Now right-click on the sheet name and select "View Code." Double-click on this workbook in the VBA code pop-up, then type the programme in the text box as shown in the image below.

Example

<div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">Private Sub <span class="token function">Worksheet_SelectionChange</span><span class="token punctuation">(</span>ByVal Target As Excel<span class="token punctuation">.</span>Range<span class="token punctuation">)</span>
   On Error GoTo <span class="token number">0</span>
   With <span class="token function">Cells</span><span class="token punctuation">(</span><span class="token function">Windows</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>ScrollRow<span class="token punctuation">,</span> <span class="token function">Windows</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>ScrollColumn<span class="token punctuation">)</span>
   'updated by nirmal
      CommandButton1<span class="token punctuation">.</span>Top <span class="token operator">=</span> <span class="token punctuation">.</span>Top <span class="token operator">+</span> <span class="token number">100</span>
      CommandButton1<span class="token punctuation">.</span>Left <span class="token operator">=</span> <span class="token punctuation">.</span>Left <span class="token operator">+</span> <span class="token number">300</span>
   End With
End Sub
</div>

Step 2

Now close the VBA code, save the file as a macro-enabled sheet, and close the workbook. The next time we open the sheet, click on the enable code to successfully complete our process, as shown in the below image.

Conclusion

In this tutorial, we used a simple example to demonstrate how you can always float a command button on a worksheet in Excel to highlight a particular set of data. The VBA code used in the sheet will only work for the ActiveX commands in Excel.

Updated on: 2023-01-12T15:07:51+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements