{"id":4183,"date":"2024-02-03T01:00:36","date_gmt":"2024-02-03T01:00:36","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/wp\/?p=4183"},"modified":"2024-02-03T01:00:36","modified_gmt":"2024-02-03T01:00:36","slug":"range-object-tutorial-in-excel-macro","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2024\/02\/range-object-tutorial-in-excel-macro\/","title":{"rendered":"Excel Macro Beginners – What is Range Object in Excel VBA"},"content":{"rendered":"
Dear readers,<\/p>\n
\nBefore we discuss the Range Object<\/strong> technically, let us see what is RANGE in excel?<\/i><\/strong> In a very simple manner if I define Range, I can say that RANGE<\/strong> is a collections of cells which are in a sequence.<\/i> This collection can have one or more than one cell.\n<\/p>\n \nAs I have defined, Range is a collection of cells, but why in a sequence? Suppose I have a combination of cells A1, B2, D7 and E9<\/strong>. Will I call this combination as a Range? No, I can not call this collection as a Range because these cells are not in a sequence. Now you know what is Range in Excel Workbook. To represent or access this cell range, the Object which is used in Excel VBA is called Range Object.<\/strong> Range Object is the most important and used Object in excel VBA. This article will give you an insight about some important and useful properties and methods of the Range Object.<\/p>\n \nAs I said above, in this article you are going to learn some useful properties and methods of Range Object. Before you go there, you should know what is Property<\/i> and <\/i>Method<\/i> of an Object in Excel VBA?<\/p>\n Property of an Object is something which has an Object. Basically it describes the Object.<\/p>\n Method of an Object is something which is performed on that Object. Typically following is the syntax of referring a Range in Excel VBA: Range(“A1:G5”)<\/p>\n <\/strong><\/p>\n Above statement will represent all the cells from A1 to G5 as shown in below pic: Excel VBA - Rang Object<\/p><\/div> With above example you can see that Range(“A1:G5”)<\/strong> is representing the whole area from Cell A1 to G5. This range is UNIQUE<\/i> in only one WorkSheet because the same range can be found in other worksheet of the same WorkBook. Therefore it becomes necessary to instruct your VBA code to access Range(“A1:G5”)<\/strong> of WHICH SHEET?<\/i> By default it assumes the range from the “Active Sheet”<\/strong>. So, if you want your code to access correct range of correct sheet then you should follow either of the below statements:<\/p>\n Let see in detail by referring to different examples.<\/p>\n As you know a Range can hold one or more than one cell. When a Range is holding a single cell like Range(“A1”)<\/strong> then it will have a single value which can be stored in a variable easily like below:<\/p>\n
\nIf I have a collection of Cells A1, B1, C1 and D1<\/strong> then I can say that this is a Range from A1<\/strong> to D1<\/strong>. Sequence of cells can be Vertical<\/strong> or Horizontal<\/strong> or both at the same time which will represent a range in a tabular format.\n<\/p>\nWhat is Property of an Object?<\/h1>\n
What is Method of an Object?<\/h1>\n
\nThis is the main difference between a property and method of an Object. Method is an action which is performed on an Object and Property is something which defines the object.\n<\/p>\nSyntax<\/h2>\n
\n<\/p>\n
\n
\n
\n <\/p>\n\n
\r\nWorksheets("SheetName").Range("A1:G5").value\r\n'***************OR*****************\r\nWorksheets("SheetName").Activate\r\nActiveSheet.Range("A1:G5").value\r\n<\/code><\/pre>\n
Examples:<\/h1>\n
How to read values from a Range Object<\/h1>\n
\r\nVal = Range(\"A1\").Value\r\n<\/code><\/pre>\n