Function to find the last used column in a sheet returning incorrectly (XL2013)

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
598
Hi there,

I have a basic function which I've used in a number of workbooks to return the last "used" column of a given worksheet

e.g. If the worksheet in question has data in the range $A$1:$Z$100, it will return 26 (referring to column Z)

If I then randomly put something in an arbitrary row (non-contiguous with the original data) in column AD, the function will return 30 (referring to column AD) etc. etc.

Pretty simple?

Here is the function :

Code:
Public Function FindLastColumn(sht As Worksheet)
    If WorksheetFunction.CountA(sht.Cells) > 0 Then
        FindLastColumn = sht.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    Else
        FindLastColumn = 1
    End If
End Function

I've just realised that if the sheet in question is filtered, and the filter has no results (there are no rows matching the criteria of the filter)

The function returns 1 - even though the header row is visible and there are multiple columns in the header row

In other words, I can see the header row in $A1:$Z1 but the function, seemingly, can't?

Why does the function not detect the populated cells of the header row when the data below is filtered out?

Thanks!

Al
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,589
Without using a function, try this code to find the last used column.
Code:
Dim lCol As Long
lCol = ActiveSheet.UsedRange.Columns.Count
 

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
598
Mmmm, don't like UsedRange to be honest - too many quirks (e.g. what if there's nothing in A1? just one example...)
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,110
Office Version
  1. 365
Platform
  1. Windows
How about starting from a different cell:

Code:
        With sht
            FindLastColumn = .Cells.Find(What:="*", After:=.Cells(1, .Columns.Count), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        End With
 
  • Like
Reactions: AOB

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
598

ADVERTISEMENT

Okay that seems to work - thanks Steve!

I'm still troubled as to why the original method fails with filtered data?

The visible header row should be sufficient for it to detect the right-most column

(Actually, even if cells are hidden, by filtering or otherwise, it should still work)

I've been using it for years across loads of workbooks - now I'm worried it's not as consistent as I thought it was
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,110
Office Version
  1. 365
Platform
  1. Windows
I cant seem to get this to fail regardless of any filtering filters or not:

Code:
Public Function FindLastColumn(sht As Worksheet)
FindLastColumn = sht.Cells.Find(What:="*", After:=sht.Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
End Function

Nope thats no good!
 
Last edited:

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,110
Office Version
  1. 365
Platform
  1. Windows
So far ive yet to see a different result using this if the filter is on or not:

Code:
Public Function FindLastColumn(sht As Worksheet)
FindLastColumn = sht.Cells.Find(What:="*", After:=sht.Cells(sht.Rows.Count, sht.Columns.Count), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,109,360
Messages
5,528,245
Members
409,809
Latest member
VICKRAM

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top