0% found this document useful (0 votes)
47 views

Select Range-Object

The document discusses the Range object in Excel VBA, which represents a cell or range of cells. It provides examples of using the Range object to select cells and ranges, declare Range variables, access rows and columns of a range, and perform actions like copying, clearing, and counting cells. Methods and properties of the Range object are described.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Select Range-Object

The document discusses the Range object in Excel VBA, which represents a cell or range of cells. It provides examples of using the Range object to select cells and ranges, declare Range variables, access rows and columns of a range, and perform actions like copying, clearing, and counting cells. Methods and properties of the Range object are described.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Excel VBA Range Object - Easy Excel Macros Page 1 of 8

Í
Excel Easy
#1 Excel tutorial on the net

Range Object
Range Examples | Cells | Declare a Range Object | Select | Rows | Columns | Copy/Paste | Clear |
Count

The Range object, which is the representation of a cell (or cells) on your worksheet, is the most important
object of Excel VBA. This chapter gives an overview of the properties and methods of the Range object.
Properties are something which an object has (they describe the object), while methods do something
(they perform an action with an object).

Range Examples
Place a command button on your worksheet and add the following code line:

Range("B3").Value = 2

Result when you click the command button on the sheet:

Code:

Range("A1:A4").Value = 5

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 2 of 8

Result:

Code:

Range("A1:A2,B3:C4").Value = 10

Result:

Note: to refer to a named range in your Excel VBA code, use a code line like this:

Range("Prices").Value = 15

Cells
Instead of Range, you can also use Cells. Using Cells is particularly useful when you want to loop
through ranges.

Code:

Cells(3, 2).Value = 2

Result:

Explanation: Excel VBA enters the value 2 into the cell at the intersection of row 3 and column 2.

Code:

Range(Cells(1, 1), Cells(4, 1)).Value = 5

Result:

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 3 of 8

Declare a Range Object


You can declare a Range object by using the keywords Dim and Set.

Code:

Dim example As Range


Set example = Range("A1:C4")

example.Value = 8

Result:

Select
An important method of the Range object is the Select method. The Select method simply selects a
range.

Code:

Dim example As Range


Set example = Range("A1:C4")

example.Select

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 4 of 8

Result:

Note: to select cells on a different worksheet, you have to activate this sheet first. For example, the
following code lines select cell B7 on the third worksheet from the left.

Worksheets(3).Activate
Worksheets(3).Range("B7").Select

Rows
The Rows property gives access to a specific row of a range.

Code:

Dim example As Range


Set example = Range("A1:C4")

example.Rows(3).Select

Result:

Note: border for illustration only.

Columns
The Columns property gives access to a specific column of a range.

Code:

Dim example As Range


Set example = Range("A1:C4")

example.Columns(2).Select

Result:

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 5 of 8

Note: border for illustration only.

Copy/Paste
The Copy and Paste method are used to copy a range and to paste it somewhere else on the worksheet.

Code:

Range("A1:A2").Select
Selection.Copy

Range("C3").Select
ActiveSheet.Paste

Result:

Although this is allowed in Excel VBA, it is much better to use the code line below which does exactly the
same.

Range("C3:C4").Value = Range("A1:A2").Value

Clear
To clear the content of an Excel range, you can use the ClearContents method.

Range("A1").ClearContents

or simply use:

Range("A1").Value = ""

Note: use the Clear method to clear the content and format of a range. Use the ClearFormats method to
clear the format only.

Count
With the Count property, you can count the number of cells, rows and columns of a range.

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 6 of 8

Note: border for illustration only.

Code:

Dim example As Range


Set example = Range("A1:C4")

MsgBox example.Count

Result:

Code:

Dim example As Range


Set example = Range("A1:C4")

MsgBox example.Rows.Count

Result:

Note: in a similar way, you can count the number of columns of a range.

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 7 of 8

Go

Chapter < >


Range Object

Learn more, it's easy


CurrentRegion

Dynamic Range

Resize

Entire Rows and Columns

Offset

From Active Cell to Last Entry

Union and Intersect

Test a Selection

Possible Football Matches

Font

Background Colors

Areas Collection

https://www.excel-easy.com/vba/range-object.html 5/28/2018
Excel VBA Range Object - Easy Excel Macros Page 8 of 8

Compare Ranges

Download Excel File


range-object.xls

Follow Excel Easy

Randomly selected
Random Numbers

RANGE OBJECT • FREE EXCEL HELP • COPYRIGHT (C) 2010-2018 • ALL RIGHTS RESERVED
MICROSOFT EXCEL TUTORIAL | HOW TO USE VBA & MACROS | EXCEL FORMULAS | FUNCTIONS IN EXCEL

https://www.excel-easy.com/vba/range-object.html 5/28/2018

You might also like