Adjusting Center Across Selection with a Cell Value

by Allen Wyatt
(last updated September 23, 2017)

5

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 sWidth As String
    Dim sStartCell As String
    Dim iWidth As Integer
    Dim r As Range
    Dim sTemp As String

    sWidth = "$A$1"
    sStartCell = "$B$1"

    If Target.Address = sWidth Then
        iWidth = Range(sWidth).Value
        If iWidth > 1 Then
            sTemp = Right(sStartCell, 1)
            sTemp = sTemp & ":" & sTemp
            Range(sTemp).HorizontalAlignment = xlGeneral

            Set r = Range(sStartCell)
            Set r = r.Resize(1, iWidth)
            r.HorizontalAlignment = xlCenterAcrossSelection
        End If
    End If
End Sub

Note that this macro is placed in the code sheet for the worksheet you want to affect. Whenever a change is made in the worksheet, the macro automatically runs. In order to make it work for you, you should change the addresses assigned to the sWidth and sStartCell variables. sWidth is set to the cell that contains how many columns you want to center across. sStartCell is set to the first cell at the left of the range where the centering is to occur.

The macro grabs whatever is in your sWidth cell and places it in the iWidth variable. If this value is greater than 1, then the centering changes take place. (It makes no sense to center across a selection that is less than 2 columns wide.) The alignment of all the cells is the row is set back to general, and then a range is defined that is just as wide as you specified, starting at the cell in sStartCell. For this range, the alignment 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, and 2016.

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

Removing All Comments

Need to get rid of all the comments in your document? You can do so by using the regular Find and Replace feature of Word.

Discover More

Understanding Lists

When designing documents there are two types of lists commonly used: numbered lists and bulleted lists. This tip introduces ...

Discover More

Extracting a State and a ZIP Code

Excel is often used to process or edit data in some way. For example, you may have a bunch of addresses from which you need ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Using Strikethrough Formatting

Need a line through the middle of your text? Use strikethrough formatting, which is easy to apply using the Format Cells ...

Discover More

Retaining Formatting After a Paste Multiply

You can use the Paste Special feature in Excel to multiple the values in a range of cells. If you don't want Excel to mess up ...

Discover More

Setting a Default Date Format

Enter a date into a cell, and Excel allows you to format that date in a variety of ways. Don't see the date format you want? ...

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}] 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 7 - 1?

2017-09-27 10:45:45

Willy Vanhaelen

@Henk
Indeed there is a little typo in the macro:
Replace
Rows(Range("SPAN").Row).HorizontalAlignment = xlGeneral
with
Rows(Range("BEGIN").Row).HorizontalAlignment = xlGeneral

The error you describe doesn't occur if "span" and "begin" are on the same row but otherwise it does.


2017-09-26 05:05:00

Henk

@Willy
It works fine if you increase the value of "span", but when you decrease it, it basically ignores the change.


2017-09-25 02:37:52

Dave Kerr

@Steve
The macro needs to be saved into the code sheet for the worksheet itself, not the module for the workbook. To access this quickly, simply right-click on the tab for the sheet and select View Code.


2017-09-24 07:24:15

Steve Jones

I copied both versions to a new module in a new workbook, saved the new workbook, enabled macros, but can't get the code to run. What am I missing?


2017-09-23 13:43:30

Willy Vanhaelen

The macro of this tip works fine if your title is in row 1 to 9 but crashes when you want your title in row 10 or any row that ends with a zero (20, 30...). For all other rows it will do nothing if you want to increase the number of columns you want to center across.

Here after is my (tiny) version that works properly. The only difference is that I do not use the sWidth and sStartCell variables. I prefer to define a range name to those cells: "span" for the cell that contains how many columns you want to center across (A1 in this tip's example) and "begin" for the first cell at the left of the range where the centering is to occur (B1 in this tip's example). This has the advantage that if you move the title you don't have to change anything in the macro code, it still works with the title in its new position. My version also removes the "center across" if you delete the span width or enter a 0 or 1 in that cell.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> Range("span").Address Then Exit Sub
Dim i As Integer
Rows(Range("span").Row).HorizontalAlignment = xlGeneral
i = Range("span").Value
Range("begin").Resize(1, i - (i = 0)).HorizontalAlignment = xlCenterAcrossSelection
End Sub


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.