Tuesday, December 22, 2015

Selection Operation on Cell

To operate the cell range selected by the user, use the Selection.

1
2
3
4
5
6
Sub Sample1()
    Dim c As Range
    For Each c In Selection
        c.Font.ColorIndex = 3
    Next c
End Sub

However, If selected object are graphs or text box, it will become an error when run in this macro. Because Selection is not a Range object

It is a trouble if you don't know what you are selected. So for the safe, instead use Selection, let use RangeSelection.

1
2
3
4
5
6
Sub Sample2()
    Dim c As Range
    For Each c In ActiveWindow.RangeSelection
        c.Font.ColorIndex = 3
    Next c
End Sub

RangeSelection is a property of the Window object. Difference selection, even  the selected object are a graph, it will return a range of cells that had been selected before.

Download File

No comments :

Post a Comment