Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Creating a Center Across Selection Button.

Creating a Center Across Selection Button

Written by Allen Wyatt (last updated August 6, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


2

Excel includes a handy tool that you can use to merge cells and center whatever is within those merged cells. (This tool is available on the Home tab of the ribbon, in the Alignment group.) The problem with this tool is that it merges prior to centering, and you might not want any merged cells in your worksheet.

Fortunately, Excel includes the capability of centering information across a range of cells, without merging them. There is no native tool on any ribbon tab that can perform this task. You can, however, display the Alignment tab of the Format Cells dialog box and use the Horizontal drop-down list to choose Center Across Selection. Doing this frequently within a worksheet can be a pain, but you can create your own tool to center information across whatever cells you've selected:

Sub CenterAcrossColumns()
    With Selection
        .HorizontalAlignment = xlCenterAcrossSelection
        .MergeCells = False
    End With
End Sub

Once you have the macro, you can assign it to a shortcut key or the Quick Access Toolbar.

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 (12183) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Creating a Center Across Selection Button.

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

Renaming a Workbook

Renaming a workbook from within Excel can seem daunting, but it is actually quite easy. All you need to do is use the ...

Discover More

Using the XIRR Function

One of the financial worksheet functions provided in Excel is the XIRR function. This is used to figure out an internal ...

Discover More

Unwanted Vertical Lines in a Table

When you print a table that includes borders, those borders should be crisp and clear on the printout. If you get some ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Converting From Numbers to Text

If you have a range of numeric values in your worksheet, you may want to change them from numbers to text values. Here's ...

Discover More

Enforcing a Desired Font

If your workbooks are shared and used by a number of different people, you may end up with some formatting in those ...

Discover More

Checking All Cell Formatting in VBA

When your macro checks the formatting used for a cell, it needs to be careful that the type of formatting being checked ...

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 3 + 2?

2023-01-29 12:18:58

J. Woolley

As with most user macros, the Tip's CenterAcrossColumns macro does not support Undo. After running the macro, pressing Ctrl+Z will not restore the selection's previous horizontal alignment. On the other hand, My Excel Toolbox includes a CenterAcrossSelection macro with support for both Undo (Ctrl+Z) and Redo/Repeat (Ctrl+Y). It can also be assigned to the Quick Access Toolbar (QAT). Here is an abbreviated version:

Public Sub CenterAcrossSelection()
    CenterAcross_Do Action:=0 ' Do
End Sub

Private Sub CenterAcross_Do(Action As Integer)
    Static WB As Workbook, WS As Worksheet, R As Range, HA As Variant
    Dim sProc As String
    Const sName As String = "CenterAcross"
    If Action = 0 Then ' Do
        Set WB = ActiveWorkbook
        Set WS = ActiveSheet
        Set R = Selection
        HA = R.HorizontalAlignment
    Else ' Undo or Redo
        WB.Activate
        WS.Activate
        R.Select
    End If
    sProc = ThisWorkbook.Name & "!" & sName
    If Action >= 0 Then ' Do or Redo
        R.HorizontalAlignment = xlHAlignCenterAcrossSelection
        Application.OnUndo ("Undo " & sName), (sProc & "_Undo")
    Else ' Undo
        R.HorizontalAlignment = HA
        Application.OnRepeat ("Redo " & sName), (sProc & "_Redo")
    End If
End Sub

Private Sub CenterAcross_Undo()
    CenterAcross_Do Action:=-1 ' Undo
End Sub

Private Sub CenterAcross_Redo()
    CenterAcross_Do Action:=1 ' Redo
End Sub

See https://sites.google.com/view/MyExcelToolbox


2022-08-06 10:21:10

J. Woolley

This is only useful if your selection is contiguous with multiple adjacent cells in the same row but only one non-blank cell in that row. The selection can include more than one row, but each row should satisfy the previous condition. For each such row, the centering begins with the non-blank cell.


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.