The last column in excel

I’ve found this method for finding the last data containing row in a sheet:

ws.Range("A65536").End(xlUp).row

Is there a similar method for finding the last data containing column in a sheet?

jamheadart's user avatar

jamheadart

4,9684 gold badges29 silver badges62 bronze badges

asked Aug 13, 2012 at 0:33

Neat Machine's user avatar

4

Lots of ways to do this. The most reliable is find.

Dim rLastCell As Range

Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)

MsgBox ("The last used column is: " & rLastCell.Column)

If you want to find the last column used in a particular row you can use:

Dim lColumn As Long

lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column

Using used range (less reliable):

Dim lColumn As Long

lColumn = ws.UsedRange.Columns.Count

Using used range wont work if you have no data in column A. See here for another issue with used range:

See Here regarding resetting used range.

Community's user avatar

answered Aug 13, 2012 at 2:00

Reafidy's user avatar

ReafidyReafidy

8,2505 gold badges49 silver badges81 bronze badges

8

I know this is old, but I’ve tested this in many ways and it hasn’t let me down yet, unless someone can tell me otherwise.

Row number

Row = ws.Cells.Find(What:="*", After:=[A1] , SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Column Letter

ColumnLetter = Split(ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Cells.Address(1, 0), "$")(0)

Column Number

ColumnNumber = ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

answered Apr 19, 2016 at 11:52

xn1's user avatar

xn1xn1

4174 silver badges12 bronze badges

Try using the code after you active the sheet:

Dim J as integer
J = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

If you use Cells.SpecialCells(xlCellTypeLastCell).Row only, the problem will be that the xlCellTypeLastCell information will not be updated unless one do a «Save file» action. But use UsedRange will always update the information in realtime.

Rob's user avatar

Rob

4,91712 gold badges52 silver badges54 bronze badges

answered May 22, 2013 at 6:55

Peter's user avatar

PeterPeter

211 bronze badge

1

I think we can modify the UsedRange code from @Readify’s answer above to get the last used column even if the starting columns are blank or not.

So this lColumn = ws.UsedRange.Columns.Count modified to

this lColumn = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1 will give reliable results always

enter image description here

?Sheet1.UsedRange.Column + Sheet1.UsedRange.Columns.Count - 1

Above line Yields 9 in the immediate window.

Community's user avatar

answered Mar 11, 2016 at 16:36

Stupid_Intern's user avatar

Stupid_InternStupid_Intern

3,3628 gold badges36 silver badges74 bronze badges

Here’s something which might be useful. Selecting the entire column based on a row containing data, in this case i am using 5th row:

Dim lColumn As Long

lColumn = ActiveSheet.Cells(5, Columns.Count).End(xlToLeft).Column
MsgBox ("The last used column is: " & lColumn)

answered Jun 27, 2018 at 9:09

dapaz's user avatar

dapazdapaz

80310 silver badges16 bronze badges

2

I have been using @Reafidy method/answer for a long time, but today I ran into an issue with the top row being merged cell from A1—>N1 and my function returning the «Last Column» as 1 not 14.

Here is my modified function now account for possibly merged cells:

Public Function Get_lRow(WS As Worksheet) As Integer
 On Error Resume Next
 If Not IsWorksheetEmpty(WS) Then
  Get_lRow = WS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Dim Cell As Range
  For Each Cell In WS.UsedRange
   If Cell.MergeCells Then
    With Cell.MergeArea
     If .Cells(.Cells.Count).Row > Get_lRow Then Get_lRow = .Cells(.Cells.Count).Row
    End With
   End If
  Next Cell
 Else
  Get_lRow = 1
 End If
End Function

Public Function Get_lCol(WS As Worksheet) As Integer
 On Error Resume Next
 If Not IsWorksheetEmpty(WS) Then
  Get_lCol = WS.Cells.Find(What:="*", after:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
  Dim Cell As Range
  For Each Cell In WS.UsedRange
   If Cell.MergeCells Then
    With Cell.MergeArea
     If .Cells(.Cells.Count).Column > Get_lCol Then Get_lCol = .Cells(.Cells.Count).Column
    End With
   End If
  Next Cell
 Else
  Get_lCol = 1
 End If
End Function

answered Nov 8, 2021 at 19:41

FreeSoftwareServers's user avatar

Here’s a simple option if your data starts in the first row.

MsgBox "Last Row: " + CStr(Application.WorksheetFunction.CountA(ActiveSheet.Cells(1).EntireRow))

It just uses CountA to count the number of columns with data in the entire row.

This has all sorts of scenarios where it won’t work, such as if you have multiple tables sharing the top row, but for a few quick & easy things it works perfect.

answered Dec 3, 2021 at 19:32

Alex Kwitny's user avatar

Alex KwitnyAlex Kwitny

10.9k2 gold badges48 silver badges70 bronze badges

Excel for Microsoft 365 Excel 2021 Excel 2019 Excel 2016 Excel 2013 Excel 2010 Excel 2007 More…Less

When you save a workbook, Excel stores only the part of each worksheet that contains data or formatting. Empty cells may contain formatting that causes the last cell in a row or column to fall outside of the range of cells that contains data. This causes the file size of the workbook to be larger than necessary and may result in more printed pages when you print the worksheet or workbook.

To avoid these issues, you can locate the last cell that contains data or formatting on a worksheet, and then reset that last cell by clearing all of the formatting that may be applied in empty rows or columns between the data and the last cell.

Locate the last cell that contains data or formatting on a worksheet

  • To locate the last cell that contains data or formatting, click anywhere in the worksheet, and then press CTRL+END.

Note: To select the very last cell in a row or column, press END, and then press the RIGHT ARROW key or the DOWN ARROW key.

Clear all formatting between the last cell and the data

  1. Do one of the following:

    • To select all columns to the right of the last column that contains data, click the first column heading, hold down CTRL, and then click the column headings of the columns that you want to select.

      Tip: You can also click the first column heading, and then press CTRL+SHIFT+END.

    • To select all rows below the last row that contains data, click the first row heading, hold down CTRL, and then click the row headings of the rows that you want to select.

      Tip: You can also click the first row heading, and then press CTRL+SHIFT+END.

  2. On the Home tab, in the Editing group, click the arrow next to the Clear button Button image, and then click Clear All.

  3. Save the worksheet.

  4. Close the worksheet.

    When you open the workbook again, the last cell of the data should be the last cell on the worksheet.

Need more help?

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

Top of Page

Need more help?


In this article, we will learn to return the Last column of data in Excel.

Scenario:

In simple words, while working with long unmannerly data, and then if needed to extract the column number of the last cell from range. We can use the below explained formula in Excel and can use the same in formulas to feed the row value. The formula considers all kinds of data types and blank cells in between range arrays.

For this article we will be needing the use the following functions:

  1. MIN function
  2. COLUMN function
  3. COLUMNS function

Now we will make a formula out of these functions. Here we will give a list of values as data. We need to find the column of the last cell value in the given input data or say the last non-blank cell in the range.

Syntax:

data : list of values having data.

Example:

All of these might be confusing to understand. So, let’s test this formula via running it on the example shown below. Here we will perform the formula over values and significance with the range data.

Use the formula:

«range» argument given as named range for the array A1:AA4 in the above formula

Explanation:

  • COLUMN function returns a range of values from the first cell column index to the last cell column index.
  • COLUMNS function returns the number of columns in the range.
  • MIN(COLUMN(range)) returns the lowest count of cells in the column in range. 
  • =MIN(COLUMN(range))+COLUMNS(range)-1 returns the last column number from the last cell.


Here the array to the function is given as the named range. Press Enter to get the last column number as result.

As you can see in the above snapshot the column number of the last non blank cell is 27. You can feed the result with other functions to extract different results.

Value from the last non blank cell.

Use the INDEX function to get the value from the last column of data.

Use the formula:


As you can see from the above formula you can get the date value from the last column as well.

Here are some observational notes using the above formula.

Notes:

  1. The formula returns the last column index as a result.
  2. The formula returns a number and can be fed to other functions as number argument.
  3. Named range in the formula be used with correct keywords.

Hope this article about How to Find the Last column of data in Excel is explanatory. Find more articles on reference formulas here. If you liked our blogs, share it with your fristarts on Facebook. And also you can follow us on Twitter and Facebook. We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write to us at info@exceltip.com.

Related Articles:

How to use the INDEX function : Return the value with the index position from the array in Excel using INDEX function.

Find the last row with mixed data in Excel : working with long unmannered numbers, text or blank cell data. Extract the last row with non blank cell using the formula in Excel.

Finding the Last Day of a Given Month : Returns the last day of a given month.

How to Get Last Value In Column : Find the last value in a column or list.

Difference with the last non blank cell : Returns the SUM of values between given dates or period in excel.

Popular Articles:

50 Excel Shortcuts to Increase Your Productivity | Get faster at your task. These 50 shortcuts will make you work ODD faster on Excel.

The VLOOKUP Function in Excel | This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets. 

COUNTIF in Excel 2016 | Count values with conditions using this amazing function. You don’t need to filter your data to count specific values. Countif function is essential to prepare your dashboard.

How to Use SUMIF Function in Excel | This is another dashboard essential function. This helps you sum up values on specific conditions.

Explanation 

When given a single cell reference, the COLUMN function returns the column number for that reference. However, when given a range that contains multiple columns, the COLUMN function will return an array that contains all column numbers for the range.

If you want only the first column number, you can use the MIN function to extract just the first column number, which will be the lowest number in the array:

=MIN(COLUMN(data)) // first column

Once we have the first column, we can add the total columns in the range and subtract 1 to get the last column number.

Index version

Instead of MIN, you can also use INDEX to get the last row number:

=COLUMN(INDEX(data,1,1))+COLUMNS(data)-1

This is possibly a bit faster for large ranges, since INDEX just supplies a single cell to COLUMN.

Simple version

When a formula returns an array result, Excel will display the first item in the array if the formula is entered in a single cell. This means that in practice, you can sometimes use a simplified version of the formula:

=COLUMN(data)+COLUMNS(data)-1

But be aware that this will return an array for a multi-column range.

Inside formulas, it’s sometimes necessary to make sure you are dealing with only one item, and not an array. In that case, you’ll want to use the full version above.

To find the last row, column, or cell you can use the range’s “End” property. The end property allows you to navigate to the end of the data range (to the last cell that is not empty). With this, there are constants that you can use to decide in which direction you want to navigate (top, bottom, left, or right).

  1. Define the cell or the range from where you want to navigate to the last row.
  2. After that, enter a dot to get the list of properties and methods.
  3. Select or type “End” and enter a starting parenthese.
  4. Use the argument that you want to use.
  5. Further, use the address property to get the address of the cell.
MsgBox Range("A1").End(xlDown).Address

When you run the above code, it shows you a message box with the row number of the last non-empty cell.

Find the Last Column using VBA

Now, let’s say you want to find the last column. In that case, instead of using “xlDown” constant, you need to use the “xlRight”, and if you want to select that cell instead of having the address then you can use the “select” method. Consider the following method.

Range("A1").End(xlToRight).Select

Find the Last Cell

By using the same method, you can also get the last cell which is a non-empty cell. To write this code, you need to know the last row and column.

Sub vba_last_row()

  Dim lRow As Long
  Dim lColumn As Long

  lRow = Range("A1").End(xlDown).Row
  lColumn = Range("A1").End(xlToRight).Column

  Cells(lRow, lColumn).Select

End Sub

To understand the above code, we need to split it into three parts.

  1. In the FIRST part, you have declared two variables to store the row and the column number.
  2. In the SECOND part, you have used “End” with the “xlDown” and then the Row property to get the row number of the last, and in the same way, you have used the “End” with the “xlToRight” and then the “Column” property to get the column number of the last column.
  3. In the THIRD part, by using the last column number and last row number refer to the last cell and select it.

Note: If you want to select a cell in the different worksheets using the last row and last column method, you need to have that worksheet activated first.

Last Row, Column, and Cell using the Find Method

You can also use the find method with the range object to get the worksheet’s last row, column, and cell. To know the row number, here is the code:

Sub vba_last_row()

Dim iRow As Long

iRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row

MsgBox iRow

End Sub

For column number:

Sub vba_last_row()

Dim iColumn As Long

iColumn = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Column

MsgBox iColumn

End Sub

To get the cell address of the last cell.

Sub vba_last_row()

Dim iColumn As Long
Dim iRow As Long

iColumn = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Column                  

iRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row                   

Cells(iRow, iColumn).Address

End Sub

More Tutorials

    • Count Rows using VBA in Excel
    • Excel VBA Font (Color, Size, Type, and Bold)
    • Excel VBA Hide and Unhide a Column or a Row
    • Excel VBA Range – Working with Range and Cells in VBA
    • Apply Borders on a Cell using VBA in Excel
    • Insert a Row using VBA in Excel
    • Merge Cells in Excel using a VBA Code
    • Select a Range/Cell using VBA in Excel
    • SELECT ALL the Cells in a Worksheet using a VBA Code
    • ActiveCell in VBA in Excel
    • Special Cells Method in VBA in Excel
    • UsedRange Property in VBA in Excel
    • VBA AutoFit (Rows, Column, or the Entire Worksheet)
    • VBA ClearContents (from a Cell, Range, or Entire Worksheet)
    • VBA Copy Range to Another Sheet + Workbook
    • VBA Enter Value in a Cell (Set, Get and Change)
    • VBA Insert Column (Single and Multiple)
    • VBA Named Range | (Static + from Selection + Dynamic)
    • VBA Range Offset
    • VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
    • VBA Wrap Text (Cell, Range, and Entire Worksheet)
    • VBA Check IF a Cell is Empty + Multiple Cells

    ⇠ Back to What is VBA in Excel

    Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes

    Понравилась статья? Поделить с друзьями:
  • The largest word in the english language
  • The largest word in dictionary
  • The largest city in the word
  • The language of men can be mastered but what kingdom word is always pronounced wrong
  • The l word шейн это то что это