Range Object In Excel VBA - Easy Dim And Set
Có thể bạn quan tâm
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 = 2Result when you click the command button on the sheet:
Code:
Range("A1:A4").Value = 5Result:
Code:
Range("A1:A2,B3:C4").Value = 10Result:
Note: to refer to a named range in your Excel VBA code, use a code line like this:
Range("Prices").Value = 15Cells
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 = 2Result:
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 = 5Result:
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 = 8Result:
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.SelectResult:
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").SelectRows
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).SelectResult:
Note: borders in the image 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).SelectResult:
Note: borders in the image for illustration only.
Copy/Paste
The Copy method and the 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.PasteResult:
Although this is allowed in Excel VBA, it is much better to use the code line below, which does exactly the same thing.
Range("C3:C4").Value = Range("A1:A2").ValueClear
To clear the content of an Excel range, you can use the ClearContents method.
Range("A1").ClearContentsor 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.
Note: borders in the image for illustration only.
Code:
Dim example As Range Set example = Range("A1:C4") MsgBox example.CountResult:
Code:
Dim example As Range Set example = Range("A1:C4") MsgBox example.Rows.CountResult:
Note: in a similar way, you can count the number of columns of a range.
If you're new here, welcome to Excel Easy! Join over 1 million monthly Excel learners. You can find popular courses here: Data Analysis in Excel and Excel VBA.
Từ khóa » Visual Basic Excel Range Select
-
How To Select Cells Or Ranges By Using Visual Basic Procedures In ...
-
Range Object (Excel) | Microsoft Docs
-
VBA Select Range / Cells - Automate Excel
-
Working With Cells And Ranges In Excel VBA (Select, Copy, Move ...
-
How To Select A Range/Cell Using VBA In Excel
-
How To SELECT ALL The Cells In A Worksheet Using A VBA Code
-
How To Use VBA To Select Range From Active Cell In Excel (3 Methods)
-
Select A Range In Excel VBA - ExcelHowto
-
Using For Each In Excel Range - Code VBA
-
Đối Tượng Range Trong Excel VBA - Viblo
-
Đối Tượng Range Trong Excel VBA - VietTuts
-
Excel VBA Range Object: 18 Useful Ways Of Referring To Cell Ranges
-
3 Ways To Select Cells And Ranges In Excel Visual Basic - WikiHow
-
Excel VBA Range Object - Guru99