Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Finding the last used column

All, FYI, I have been using the following (offered by Jim Thomlinson in 2006)
without trouble for years:
Function fnLastCol(sh As Worksheet) As Long
On Error Resume Next
fnLastCol = sh.Cells.Find(what:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function

Recently however, I received a worksheet from a colleague structured as:
A:AK populated, AL:AY empty, AZ:BA populated
So the function should have returned 53 (BA) but was in fact returning 37
(AK) .

Only when I noticed that columns J:AK were grouped and removed the grouping
could I make the function behave as I expected.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Finding the last used column

Consider:

Function fnLastCol(sh As Worksheet) As Long
fnLastCol = 0
n = Columns.Count
For i = n To 1 Step -1
If Application.WorksheetFunction.CountA(sh.Columns(i) ) < 0 Then
fnLastCol = i
Exit Function
End If
Next
End Function

tested with:

Sub main()
Dim sh As Worksheet
Set sh = Sheets("Sheet1")
MsgBox (fnLastCol(sh))
End Sub

--
Gary''s Student - gsnu2007k


"Lionel H" wrote:

All, FYI, I have been using the following (offered by Jim Thomlinson in 2006)
without trouble for years:
Function fnLastCol(sh As Worksheet) As Long
On Error Resume Next
fnLastCol = sh.Cells.Find(what:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function

Recently however, I received a worksheet from a colleague structured as:
A:AK populated, AL:AY empty, AZ:BA populated
So the function should have returned 53 (BA) but was in fact returning 37
(AK) .

Only when I noticed that columns J:AK were grouped and removed the grouping
could I make the function behave as I expected.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Finding the last used column

I can not get it to return anything but 53... that being said you can try
changing xlFormulas to xlValues and see if that helps...

Function fnLastCol(sh As Worksheet) As Long
On Error Resume Next
fnLastCol = sh.Cells.Find(what:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function


--
HTH...

Jim Thomlinson


"Lionel H" wrote:

All, FYI, I have been using the following (offered by Jim Thomlinson in 2006)
without trouble for years:
Function fnLastCol(sh As Worksheet) As Long
On Error Resume Next
fnLastCol = sh.Cells.Find(what:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function

Recently however, I received a worksheet from a colleague structured as:
A:AK populated, AL:AY empty, AZ:BA populated
So the function should have returned 53 (BA) but was in fact returning 37
(AK) .

Only when I noticed that columns J:AK were grouped and removed the grouping
could I make the function behave as I expected.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
finding the largest value for a name in a column and then returningthe result from a different column [email protected] Excel Worksheet Functions 1 April 14th 08 10:08 AM
Finding Info from Column A and Removing it from Column B Johnny B[_2_] Excel Discussion (Misc queries) 2 March 28th 07 12:06 PM
Finding the closest number in column A and take the value in column B reefguy Excel Worksheet Functions 3 May 5th 06 07:25 PM
Output from a userform finding the right column. column HelpMe Excel Programming 1 February 10th 04 03:27 PM
finding and reporting in column A, where a series of column reaches zero Gary Tamblyn Excel Programming 2 July 27th 03 12:00 PM


All times are GMT +1. The time now is 11:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"