Adjusting Center Across Selection with a Cell Value

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


Jean likes using the "Center Across Selection" setting on the Alignment tab of the Format Cells dialog box to center information across un-merged cells. She wonders, though, if there is a way she can use the contents of one cell to control across how many cells the centering occurs. For instance, if she has the number 4 in cell A1, then the centering would be across 4 cells (B1:E1), but if she changes it to 5 then the centering would be across 5 cells (B1:F1).

The only way this can be done is through the use of a macro. Because you might change the value in cell A1, the macro needs to run whenever you make a change to the workbook and then determine if that change was made in cell A1 or not. If so, then it can make the adjustment to the cells in the row.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range

    If Target.Address = "$A$1" Then
        Set r = Range("B1")
        r.EntireRow.HorizontalAlignment = xlGeneral
        r.Resize(1, Target).HorizontalAlignment = xlCenterAcrossSelection
    End If
End Sub

Note that this macro is placed in the code sheet for the worksheet you want to affect. (Right-click the worksheet's tab and choose View Code.) Whenever a change is made in the worksheet, the macro automatically runs. To make it work for your needs, change "$A$1" to the address of the cell that contains how many columns you want to center across. (This needs to be a full address, including the dollar signs.) You'll also want to change "B1" to the address of the first cell at the left of the range where the centering is to occur. (This address does not require the dollar signs.)

The macro sets the value of the r Range variable to your starting cell. It then sets the alignment of all the cells in the row back to general. Finally, the size of the range is adjusted to be just as wide as you specified and the alignment of the range is set to xlCenterAcrossSelection to give the desired results.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (4360) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Faster Picture Displaying

If you have a document with many graphics, you know that Word can sometimes be slow in displaying all those graphics. ...

Discover More

Notation for Thousands and Millions

When working with very large numbers in a worksheet, you may want the numbers to appear in a shortened notation, with an ...

Discover More

Cannot Open Multiple Word Documents

What are you to do if you try to open a document and Word automatically closes your previous document? Word is not ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Partially Blocking Social Security Numbers

Need to protect a series of Social Security Numbers in a worksheet? The techniques provided in this tip might be a good ...

Discover More

Shrinking Cell Contents

Need to cram a bunch of text all on a single line in a cell? You can do it with one of the lesser-known settings in Excel.

Discover More

Accurate Font Sizes

Need to use some bizarre font size in your worksheet? Not a problem, provided it is a full or half point size.

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 two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.