Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Selecting Columns in VBA when Cells are Merged.

Selecting Columns in VBA when Cells are Merged

by Allen Wyatt
(last updated June 9, 2017)

4

Say you have a blank worksheet and the range A1:F1 has the "merge and center" format applied to it. If you select column B by clicking the column heading, Excel dutifully selects column B and makes cell B2 the active cell. This behavior was modified in either Excel 2000 or Excel 2002; in previous versions of Excel you get the merged cell (A1:F1) included in the selection.

Apparently VBA trails somewhat behind the behavior of the user interface, as selecting the entire column B also ends up selecting all the columns, A through F:

Sub TestMacro1()
    Range("B3").EntireColumn.Select
End Sub

There seems to be no way around this behavior. Even if you eliminate the EntireColumn method and simply select column B, you still get all the columns, A through F:

Sub TestMacro2()
    Range("B:B").Select
End Sub

It is probably a better programming approach to not select the column preparatory to doing some action upon that column, but to do the action directly. For instance, let's assume that you want to make all of the cells in column B bold. You can do so in this manner:

Sub TestMacro3()
    Range("B3").EntireColumn.Font.Bold = True
End Sub

This affects only the cells in column B, and nothing in A or C through F. You could similarly use an iterative approach to processing the cells in the desired column:

Sub TestMacro4()
    Dim rCell As Range
    Dim X As Long

    X = 1
    For Each rCell In Range("B:B")
        rCell.Value = X
        X = X + 1
    Next
End Sub

This stuffs a value into each cell in column B, and conveniently ignores any merges that include a cell in column B.

If it is mandatory that you be able to select an entire column, without any columns added because of merged cells, then you may be tempted to use the MergeCells property to check for the merged cells. According to the VBA online help, the following should detect the merged cells in the selection and then dump out of the macro:

Sub TestMacro5()
    Range("B3").EntireColumn.Select
    If Selection.MergeCells Then
        Exit Sub
    End If
'
' Perform rest of macro
'
End Sub

Unfortunately, testing shows that this code will not work. The MergeCells property apparently only returns True if the entire selection is made up of merged cells, not if the selection only contains a few merged cells. That means that you are left to some other way to determine if merged cells have modified the intended selection, such as the following:

Sub TestMacro6()
    Range("B3").EntireColumn.Select
    If Selection.Columns.Count > 1 Then
        Exit Sub
    End If
'
' Perform rest of macro
'
End Sub

This approach examines the number of columns in the selection, and then dumps out if Excel reports that there is more than one.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3093) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Selecting Columns in VBA when Cells are Merged.

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

Defining a Name

One of the great features of Excel is that it allows you to use named ranges. These can make your formulas much easier to ...

Discover More

Adding Diagonal Borders

Borders on all sides of a cell are easy to do in Excel. You can also create diagonal borders that run right through the ...

Discover More

Progression Indicator in a Macro

When your macro is humming along, minding its own business, a user watching the screen may not see any activity and therefore ...

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 (menu)

Determining the Hour of the Day

Need to know the current hour of the day? You can derive the information in your macros by using the Hour function, as ...

Discover More

Assigning Macros to Graphics

The graphics you place in a worksheet can do more than just look pretty. You can also assign macros to a graphic, which are ...

Discover More

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference in ...

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 8Mpixels. 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 5 + 0?

2017-08-11 17:27:34

Chandra

Hi,

I am trying to merge cells in sheet2. But when I give the range, cell reference in Sheet1 gets activated. Even if I activate Sheet2 & select the range to merge cells, it doesn't work. When I try to display the active sheet & active cell reference, it displays the Sheet2 as active sheet.

This is the code I am trying to use.
Range(ActiveCell.Address, Cells(x - 1, "A")).Merge

But this merges cells in Sheet1 while the active sheet is Sheet 2. Can you pls help?


2016-02-02 14:59:48

William Glasby

I am running into this issue when trying to "group" columns into an "outline", for example...

The range "E1:J1" is a merged & centered cell. I want to group the range "E:G". Using VBA, I try to select "E:G" and so I can group those columns, but it always selects "E:J" instead.

Any workaround for this?

Thanks,
William


2015-05-08 13:15:32

AJ

Accessing columns directly works when you only have 1 sheet you are trying to effect. However when I have multiple sheets I want to effect (doing a find and replace on columns A:B) using your method

Range("B3").EntireColumn.Font.Bold

only effects range B3 on the active sheet.

Is there a better solution for changing all sheets in a selection?


2013-01-27 15:08:07

Heather Heckman

Your Sub Test Macro 4 really saved me in dealing with a merged range that needed to have "0" added throughout the specific range. Thank you.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.