Table of Contents
VBA, or Visual Basic for Applications, is a programming language commonly used in Microsoft Office applications such as Excel. It allows users to automate tasks and manipulate data within the program. One useful function of VBA is the ability to find the last used column in a worksheet.
To use VBA to find the last used column in a worksheet, you can use the “End” method. This method allows you to specify the direction in which you want to search for data. By using the “xlToLeft” direction, the code will start at the last column and scan left until it reaches the first column with data.
Another option is to use the “Find” method, which allows you to search for specific data within a range. By searching for the last column with data, you can determine the last used column in the worksheet.
Using VBA to find the last used column in a worksheet can save time and improve efficiency, especially when dealing with large amounts of data. It eliminates the need for manual counting and ensures accuracy in determining the last column with data. This function can be applied in various scenarios, such as data analysis, report generation, and data entry tasks.
VBA: Find Last Used Column
You can use the following basic syntax in VBA to find the last column used in an Excel sheet:
Sub FindLastColumn()
Range("A14") = Cells.Find("*",Range("A1"),xlFormulas,xlPart,xlByColumns,xlPrevious,False).Column
End SubThis particular example finds the last used column in the current sheet and returns the result in cell A14.
If you would instead like to display the last column in a message box, you can use the following syntax:
Sub FindLastColumn()
Dim LastCol As Long
LastCol=Cells.Find("*",Range("A1"),xlFormulas,xlPart,xlByColumns,xlPrevious,False).Column
MsgBox "Last Column: " & LastCol
End Sub
The following examples shows how to use each of these methods in practice.
Example 1: Find Last Column Using VBA and Display Results in Cell
Suppose we have the following dataset in Excel that contains information about various basketball players:

We can create the following macro to find the last column used in this Excel sheet and display the result in cell A14:
Sub FindLastColumn()
Range("A14") = Cells.Find("*",Range("A1"),xlFormulas,xlPart,xlByColumns,xlPrevious,False).Column
End SubWhen we run this macro, we receive the following output:

Notice that cell A14 contains a value of 2.
This tells us that the last used column in this particular sheet is column 2.
It’s also worth noting that if you have empty columns before a used column, this macro will still find the last used column.

Cell A14 contains a value of 5 because this is the last column with values in it.
Example 2: Find Last Column Using VBA and Display Results in Message Box
Suppose we would instead like to find the last used column in a sheet and display the column number in a message box.
We can create the following macro to do so:
Sub FindLastColumn()
Dim LastCol As Long
LastCol=Cells.Find("*",Range("A1"),xlFormulas,xlPart,xlByColumns,xlPrevious,False).Column
MsgBox "Last Column: " & LastCol
End SubWhen we run this macro, we receive the following output:

The message box tells us that the last used column in the sheet is column 2.
Note: You can find the complete documentation for the VBA Find method .
Cite this article
stats writer (2024). How can I use VBA to find the last used column in a worksheet?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-column-in-a-worksheet/
stats writer. "How can I use VBA to find the last used column in a worksheet?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-column-in-a-worksheet/.
stats writer. "How can I use VBA to find the last used column in a worksheet?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-column-in-a-worksheet/.
stats writer (2024) 'How can I use VBA to find the last used column in a worksheet?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-column-in-a-worksheet/.
[1] stats writer, "How can I use VBA to find the last used column in a worksheet?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use VBA to find the last used column in a worksheet?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
