How To Display a CommandBar centered on the screen using VBA in Excel?
Tags: todisplayacommandbar, vbamacros
Published: 2021-10-19 02:56:51

---
title: How To Display a CommandBar centered on the screen using VBA in Excel?
published: true
date: 2021-10-15 11:05:57 UTC
tags: ToDisplayACommandBar,VBAMacros
canonical_url: https://geekexcel.com/how-to-display-a-commandbar-centered-on-the-screen-using-vba-in-excel/
---

In this tutorial, you will learn how to  **display a CommandBar centered on the screen using VBA**  in Microsoft Excel. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## Example

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Declare Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

Sub CenterCommandBar()
Dim w As Long, h As Long
    w = GetSystemMetrics32(0) ' screenwidth in points
    h = GetSystemMetrics32(1) ' screenheight in points
    With CommandBars("MyCommandBarName")
        .Position = msoBarFloating
        .Left = w / 2 - .Width / 2
        .Top = h / 2 - .Height / 2
    End With
End Sub

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Save-the-Code-44.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- Then, you need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Run-the-Code-36.png)<figcaption>Run the Code</figcaption>

- Finally, after running the macro, you can see the result as a **CommandBar** displayed at **centered on the screen** , both horizontally and vertically.

## Wrap-Up

We hope that this tutorial gives you guidelines on how to  **display a CommandBar centered on the screen using VBA**  in Microsoft Excel. ** ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Further Reference:**

- **[Shortcut to Move One Screen Right in MS Excel 365!- A Simple Shortcuts](https://geekexcel.com/shortcut-to-move-one-screen-right-in-ms-excel-365/)**
- **[How to Move One Screen Left Using Shortcuts in Excel 365?](https://geekexcel.com/easy-shortcut-to-move-one-screen-left-in-microsoft-excel-365/)**
- **[Simple Shortcut to “Move One Screen Up” in Excel 365!](https://geekexcel.com/simple-shortcut-to-move-one-screen-up-in-excel-365/)**
- **[MOVE ONE SCREEN DOWN using Shortcuts in Excel Spreadsheet!!](https://geekexcel.com/excel-keyboard-shortcut-to-move-one-screen-down-in-spreadsheet/)**
- **[Extend Selection Up One Screen – Easy Shortcut in Excel 365!!](https://geekexcel.com/shortcut-to-extend-selection-up-one-screen-in-excel-worksheet/)**