
Tips.Net > ExcelTips Home > Macros > Selecting Columns in VBA when Cells are Merged
Summary: If you manually select an entire column, and there are some cells in that column that are merged with cells in an adjacent column, Excel sort of “skips” the merged cells in the selection. Not so in VBA, where selecting the entire column results in selecting all the columns affected by the merged cells. This tip explains why this happens and presents some other options about how to work with entire columns that contain merged cells. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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.
Tip #3093 applies to Microsoft Excel versions: 97 2000 2002 2003
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
Check out ExcelTips: The Macros today!
Thousands of ExcelTips, available for immediate download. Have all the Microsoft Excel info you need, right at your fingertips. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site