End Function in VBA
For everything, there is an end. In VBA, it is no different. You must have seen the word “End” in all the codes in your VBA. We can End in “End Sub,” “End Function,” and “End If.” These are common as we know each “End” suggests the end of the procedure. These VBA End statements do not require any special introduction because we are familiar with them in our VBA codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more.
Apart from the above “End,” we have one property, “End,” in VBA. This article will take you through that property and how to use it in our coding.
You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA End (wallstreetmojo.com)
End Property in VBA
The “End” is the property we use in VBA to move in the suggested direction. The typical example of direction is moving from the active cell to the last used cell or the last entry cell horizontally and vertically in the worksheet.
For example, let us recall this with a worksheet. Look at the below image.
Right now, we are in the A1 cell.
If we want to move to the last used cell horizontally, we use the excel shortcut keyUse The Excel Shortcut KeyAn Excel shortcut is a technique of performing a manual task in a quicker way.read more Ctrl + Right Arrow, which will take us to the last used cell horizontally.
Similarly, if we want to move to the last used cell downwards or vertically, we press the shortcut key Ctrl + Down Arrow.
So, to move from left to right, we press Ctrl + Left Arrow. To move from bottom to top, we press Ctrl + Up Arrow.
A similar thing can be done in VBA but not by using the Ctrl key. Rather, we need to use the word “End.”
Examples of Excel VBA End Function
Example #1 – Use VBA End Property To Move in Worksheet
Let us look at how to use Excel VBA End to move in the sheet. First, we need to decide which cell we need to move. For example, suppose we need to move from cell A1, so refer to the cell using the VBA Range objectUsing The VBA Range ObjectRange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more.
Code:
Sub End_Example1()
Range (“A1”)
End Sub
Put dot (.) to see the IntelliSense list. Then, select “End” VBA property from the list.
Sub End_Example1()
Range(“A1”).End
End Sub
Once the end property is selected, open parenthesis.
Sub End_Example1()
Range(“A1”).End(
End Sub
As soon as you open parenthesis, we can see all the available options with the “End” property. Select “xlToRight” to move horizontally from cell A1 to the last used cell.
Sub End_Example1()
Range(“A1”).End (xlToRight)
End Sub
After moving to the last cell, we must select what we need to do. Put dot (.) to see the IntelliSense list.
Sub End_Example1()
Range(“A1”).End(xlToRight).
End Sub
Choose the “Select” method from the IntelliSense list.
Sub End_Example1()
Range(“A1”).End(xlToRight).Select
End Sub
It will make use of cell A1 to last used cells horizontally.
Similarly, use the other three options to move right, left, down, and up.
To move right from cell A1.
To move down from cell A1.
Sub End_Example1()
Range(“A1”).End(xlDown).Select
End Sub
To move up from cell A5.
Sub End_Example1()
Range(“A5”).End(xlUp).Select
End Sub
To move left from cell D1.
Sub End_Example1()
Range(“D1”).End(xlToLeft).Select
End Sub
All the above codes are examples of using the “End” property to move in the worksheet.
We will now see how to select the ranges using the “End” property.
Example #2 – Selection Using End Property
We need to end the property to select the range of cells in the worksheet. For this example, consider the below data.
Select A1 to Last Used Cell
To select the cells from A1 to the last used cell horizontally, first, mention cell A1 in the Range object.
Sub End_Example2()
Range(“A1”,
End Sub
For the second argument, open one more Range object and mention the cell as A1 only.
Sub End_Example2()
Range(“A1”,Range(“A1”)
End Sub
Close only one bracket and put a dot to select the Excel VBA End property.
Sub End_Example2()
Range(“A1”,Range(“A1”).End(
End Sub
Now, select xlToRight and close two brackets.
Sub End_Example2()
Range(“A1”,Range(“A1”).End(xlToRight))
End Sub
Now, choose the “Select” method.
Sub End_Example2()
Range(“A1”, Range(“A1”).End(xlToRight)).Select
End Sub
We have completed it now.
Run this code to see the impact.
As you can see, it has selected the range A1 to D1.
Similarly, to select downwards, use the below code.
Sub End_Example2()
Range(“A1”, Range(“A1”).End(xlDown)).Select ‘To select from left to right End Sub
Sub End_Example2()
Range(“A1”, Range(“A1”).End(xlDown)).Select ‘To select from top to down End Sub
Sub End_Example2()
Range(“D1”, Range(“D1”).End(xlToLeft)).Select ‘To select from right to left End Sub
Sub End_Example2()
Range(“A5”, Range(“A5”).End(xlUp)).Select ‘To select from bottom to up End Sub
Example #3 – Select Right to Left, Right to Bottom, & Top
We have seen how to select horizontally and vertically. Next, we need to use two “End” properties to select vertically and horizontally. For example, to select the data from A1 to D5, we need to use the below code.
Sub End_Example3()
Range(“A1”, Range(“A1”).End(xlDown).End(xlToRight)).Select ‘To from cell A1 to last use cell downwards & rightwards End Sub
It will select the complete range like the one below.
Like this, we can use the VBA “End” function property to select a range of cells.
Recommended Articles
This article has been a guide to the Excel VBA End function. Here, we learn how to use End property in VBA and End property selection, examples, and a downloadable template. Below are some useful Excel articles related to VBA: –
- VBA SplitCall Sub in ExcelToday in VBAHow to Paste in VBA?