Select Cell Range in Excel VBA

Description:

If we want to deal with Cell or Range we can directly mention the object and change its properties or call its methods. We can also deal with Cells or Range by simply selecting it. Then we can use Selection object to refer Selected Cells, Range or any other Objects.

We select cell range in Excel VBA to do particular task with the selection. For example we may want to change the background color of a range and we may want to change the font to Bold. In this case we do not required to loop each and every cell of the range to change the background color and font.

Select Cell Range in Excel VBA – Solution:

Select Cell Range in Excel VBA
We can use Select method of Range or Cell to select it and do whatever you want to do.

Select Cell Range in Excel VBA – Example:

Following are the various examples which will show you how to select a range and perform some task.

Example to Select a Cell

In this example I am selecting a Cell using Select method of Cell.

Sub sbSelectACell()

'Selecting a Cell
Cells(2, 3).Select 'This will select the Cell at 2nd row and 3rd column

End Sub
Example to Select a Range

In this example I am selecting a Range using Select method of Range.

Sub sbSelectARange()
'You can also use Range Object
Range("C3").Select

'Collection of Cells OR Multiple Cells = Range
Range ("B2:C4").Select ' It will Select B2,B3,B4,C2,C3,C4

End Sub
Example to Select a Range and change the background color

In this example I am selecting the range from B2:C4 using Select method. And changing the background color to red using ColorIndex Property of Range.

Sub sbSelectARangeandForamt()
'Selecting a Range
Range("B2:C4").Select  ' It will Select B2,B3,B4,C2,C3,C4
Selection.Interior.ColorIndex = 3

End Sub

Instructions
  1. Open an Excel Workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a Module from Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. Press F5 to execute it

Valli

Excel VBA Developer having around 8 years of experience in using Excel and VBA for automating the daily tasks, reports generation and dashboards preparation. Valli is sharing to helps us automating daily tasks.

You may also like...

2 Responses

  1. Mukesh says:

    Good Excellent

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>