Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default last used cell function

I think I saw in this forum a while back a solution for finding the last used
cell in a worksheet in the form of a function that gets passed to it the name
of the worksheet (I think) and returns a range that is the last used cell in
that worksheet (this I'm sure). The trouble is I can't seem to be able to
find it anymore.

The solution looks very similar to Jim Thomlinson's response to post "Find
last used column" dated 4/6/2006 except the function returns a range as noted
above.

If you can tell me where to find it, or provide the solution itself, that
would be great.
Thanks.





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default last used cell function

Not too sure what I posted way back then but then again I can't remember what
I did last weekend... How about something like this...

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
Dim lngLastRow As Long
Dim intLastColumn As Integer

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
intLastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
intLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, intLastColumn)
End Function

This returns the last used cell on a sheet. If no sheet is supplied then it
uses the active sheet. Try it something like this...

sub test
msgbox LastCell(Sheets("Sheet1")).Address
msgbox LastCell(Sheets("Sheet2")).Value
msgbox LastCell().Address
end sub

--
HTH...

Jim Thomlinson


"rockhammer" wrote:

I think I saw in this forum a while back a solution for finding the last used
cell in a worksheet in the form of a function that gets passed to it the name
of the worksheet (I think) and returns a range that is the last used cell in
that worksheet (this I'm sure). The trouble is I can't seem to be able to
find it anymore.

The solution looks very similar to Jim Thomlinson's response to post "Find
last used column" dated 4/6/2006 except the function returns a range as noted
above.

If you can tell me where to find it, or provide the solution itself, that
would be great.
Thanks.





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default last used cell function

Not bad.

I have trouble with "before lunch today"<g


Gord Dibben MS Excel MVP

On Mon, 1 Oct 2007 17:08:01 -0700, Jim Thomlinson
wrote:

Not too sure what I posted way back then but then again I can't remember what
I did last weekend


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default last used cell function

Hi Jim, thanks for the quick reply. That must be it. If fact, I just
dicovered by searching your name that you posted that on 9/1/2006.


"Jim Thomlinson" wrote:

Not too sure what I posted way back then but then again I can't remember what
I did last weekend... How about something like this...

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
Dim lngLastRow As Long
Dim intLastColumn As Integer

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
intLastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
intLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, intLastColumn)
End Function

This returns the last used cell on a sheet. If no sheet is supplied then it
uses the active sheet. Try it something like this...

sub test
msgbox LastCell(Sheets("Sheet1")).Address
msgbox LastCell(Sheets("Sheet2")).Value
msgbox LastCell().Address
end sub

--
HTH...

Jim Thomlinson


"rockhammer" wrote:

I think I saw in this forum a while back a solution for finding the last used
cell in a worksheet in the form of a function that gets passed to it the name
of the worksheet (I think) and returns a range that is the last used cell in
that worksheet (this I'm sure). The trouble is I can't seem to be able to
find it anymore.

The solution looks very similar to Jim Thomlinson's response to post "Find
last used column" dated 4/6/2006 except the function returns a range as noted
above.

If you can tell me where to find it, or provide the solution itself, that
would be great.
Thanks.





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
ISBLANK function not working when cell is blank dut to function re mcmilja Excel Discussion (Misc queries) 9 May 7th 23 03:43 AM
Copy Excel Function from a Cell Through a Function Sheikh Saadi Excel Worksheet Functions 0 October 15th 09 04:22 PM
I want result of a function in the cell and not the function itsel Rana Excel Worksheet Functions 4 July 2nd 07 06:20 AM
Can function in one cell change value or function in another cell? me Excel Worksheet Functions 4 February 27th 06 01:04 PM
Custom Function: Detecting the cell the function is used in g-boy Excel Programming 2 June 11th 05 06:46 PM


All times are GMT +1. The time now is 02:41 PM.

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

About Us

"It's about Microsoft Excel"