VBA methods in Excel
In the Write/Read Cell, Objects Chapter, we will see in detail about objects and its properties.
The properties just defines what objects are and does not utilize the object's functionalities.
Every object performs some actions and those actions are contained in its methods.
Thus, Methods are the actions that can be performed by the objects.
For example, a Range object can be used to Copy contents from one range to another or from one sheet to another.
This action of Copying and Pasting is an action and is performed by VBA METHODS called "Copy" and "Paste".
So, a VBA method makes the object meaningful. Without methods, objects are meaningless.
The fact that, VBA is not fully object oriented is seen from the fact that, objects are not instantiated in VBA, unlike in other OOPS languages, where instantiating objects is a must.
The following example illustrates the concepts discussed above. We named a new Sheet Q26.
- Sub Methods_example()
- Dim rng As Range
- Set rng = Sheets("Q26").Range("P15")
- rng.Value = "Hello"
- rng.Copy
- Sheets("Q26").Range("P26").Select
- ActiveSheet.Paste
- MsgBox "Copied from P15 and Pasted to P26"
- rng.Clear
- Sheets("Q26").Range("P26").Clear
- End Sub
The screenshot of the VBA editor is as shown below.
The rng.copy and the ActiveSheet.Paste are both VBA Methods.
The above example Copies the Value in P15 and Pastes to P26.
And a message box is displayed for you to confirm, as shown below.