Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Find the last used Row on a Worksheet

Here's an example I found on the web
---

Find the last used Row on a Worksheet:

Sub FindLastRow()
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) 0 Then

'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows,SearchDirection:=xlPrevious) .Row

MsgBox LastRow
End If

End Sub

---

How do you tell Sub FindLastRow what worksheet to use ?

Also, is the above an OK method ?

Thanks - Kirk
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Find the last used Row on a Worksheet

This was my solution last week. It take a couple of seconds to run, but it
is a straight forward way of finding last row that I can remember. I don't
like to remember code unless I use it often.

Sub Lastrow()

For RowCount = Rows.Count To 1 Step -1
LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column
If (LastCol < 1) Or (Not IsEmpty(Cells(RowCount, "A"))) Then
Exit For
End If
Next RowCount
MyLastRow = RowCount
End Sub

"kirkm" wrote:

Here's an example I found on the web
---

Find the last used Row on a Worksheet:

Sub FindLastRow()
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) 0 Then

'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows,SearchDirection:=xlPrevious) .Row

MsgBox LastRow
End If

End Sub

---

How do you tell Sub FindLastRow what worksheet to use ?

Also, is the above an OK method ?

Thanks - Kirk

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 695
Default Find the last used Row on a Worksheet

in column A try

MsgBox ("") & Cells(65536, "A").End(xlUp).Row


"Joel" skrev:

This was my solution last week. It take a couple of seconds to run, but it
is a straight forward way of finding last row that I can remember. I don't
like to remember code unless I use it often.

Sub Lastrow()

For RowCount = Rows.Count To 1 Step -1
LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column
If (LastCol < 1) Or (Not IsEmpty(Cells(RowCount, "A"))) Then
Exit For
End If
Next RowCount
MyLastRow = RowCount
End Sub

"kirkm" wrote:

Here's an example I found on the web
---

Find the last used Row on a Worksheet:

Sub FindLastRow()
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) 0 Then

'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows,SearchDirection:=xlPrevious) .Row

MsgBox LastRow
End If

End Sub

---

How do you tell Sub FindLastRow what worksheet to use ?

Also, is the above an OK method ?

Thanks - Kirk

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 695
Default Find the last used Row on a Worksheet

or
MsgBox ("") & Sheets("Sheet1").Cells(65536, 1).End(xlUp).Row

"Joel" skrev:

This was my solution last week. It take a couple of seconds to run, but it
is a straight forward way of finding last row that I can remember. I don't
like to remember code unless I use it often.

Sub Lastrow()

For RowCount = Rows.Count To 1 Step -1
LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column
If (LastCol < 1) Or (Not IsEmpty(Cells(RowCount, "A"))) Then
Exit For
End If
Next RowCount
MyLastRow = RowCount
End Sub

"kirkm" wrote:

Here's an example I found on the web
---

Find the last used Row on a Worksheet:

Sub FindLastRow()
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) 0 Then

'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows,SearchDirection:=xlPrevious) .Row

MsgBox LastRow
End If

End Sub

---

How do you tell Sub FindLastRow what worksheet to use ?

Also, is the above an OK method ?

Thanks - Kirk

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Find the last used Row on a Worksheet

See
http://www.rondebruin.nl/last.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"kirkm" wrote in message ...
Here's an example I found on the web
---

Find the last used Row on a Worksheet:

Sub FindLastRow()
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) 0 Then

'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows,SearchDirection:=xlPrevious) .Row

MsgBox LastRow
End If

End Sub

---

How do you tell Sub FindLastRow what worksheet to use ?

Also, is the above an OK method ?

Thanks - Kirk



  #6   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Find the last used Row on a Worksheet

If you want to modify your code to search a specific sheet you need to
qualify your range references with the worksheet

Sub FindLastRow()
Dim LastRow As Long

If WorksheetFunction.CountA(Worksheets("Sheet2").Cell s) 0 Then
'Search for any entry, by searching backwards by Rows.
LastRow = Worksheets("Sheet2").Cells.Find(What:="*", _
After:=Worksheets("Sheet2").[A1], _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox LastRow
End If

End Sub


But, it is better to use a With statement for this

Sub FindLastRow()
Dim LastRow As Long

With Worksheets("Sheet2")
If WorksheetFunction.CountA(.Cells) 0 Then
'Search for any entry, by searching backwards by Rows.
LastRow = .Cells.Find(What:="*", After:=.[A1], _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox LastRow
End If
End With
End Sub


Of course, this code will work on the active workbook. If you needed to
specify which workbook, you would need to qualify your range references
further.

"kirkm" wrote:

Here's an example I found on the web
---

Find the last used Row on a Worksheet:

Sub FindLastRow()
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) 0 Then

'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows,SearchDirection:=xlPrevious) .Row

MsgBox LastRow
End If

End Sub

---

How do you tell Sub FindLastRow what worksheet to use ?

Also, is the above an OK method ?

Thanks - Kirk

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Find the last used Row on a Worksheet

Thanks one and all for the help and
code exapmples. Much appreciated & problem solved.
Learnt some valuable stuff too :)
Cheers - Kirk
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 256
Default Find the last used Row on a Worksheet

On Apr 29, 11:35 pm, kirkm
wrote:
Thanks one and all for the help and
code exapmples. Much appreciated & problem solved.
Learnt some valuable stuff too :)
Cheers - Kirk


Dim rngLastRow as Excel.Range

With ActiveSheet
Set rngLastRow
= .Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Co unt).EntireRow
End With

  #9   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Find the last used Row on a Worksheet

One caveat - the usedrange is not always the last cell that actually has data
in it. For example, if a cell below the last cell w/data has formatting
applied, the formatted cell will be the last cell in the usedrange even
though it is empty.


"ilia" wrote:

On Apr 29, 11:35 pm, kirkm
wrote:
Thanks one and all for the help and
code exapmples. Much appreciated & problem solved.
Learnt some valuable stuff too :)
Cheers - Kirk


Dim rngLastRow as Excel.Range

With ActiveSheet
Set rngLastRow
= .Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Co unt).EntireRow
End With


  #10   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Find the last used Row on a Worksheet

One caveat - the usedrange is not always the last cell that actually has data
in it. For example, if a cell below the last cell w/data has formatting
applied, the formatted cell will be the last cell in the usedrange even
though it is empty.

What could be problematic, is if the usedrange is say D5:F7 (as these are
the only cells that ever had any data in them), your code will select Row 3
(because there are only 3 rows in the usedrange). I think you would need to
change it to:

With ActiveSheet
Set rngLastRow =
..UsedRange.Cells(.UsedRange.Rows.Count,.UsedRange .Columns.Count).EntireRow
End With

but that assumes that the first caveat does not create any problems.

"ilia" wrote:

On Apr 29, 11:35 pm, kirkm
wrote:
Thanks one and all for the help and
code exapmples. Much appreciated & problem solved.
Learnt some valuable stuff too :)
Cheers - Kirk


Dim rngLastRow as Excel.Range

With ActiveSheet
Set rngLastRow
= .Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Co unt).EntireRow
End With




  #11   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Find the last used Row on a Worksheet

One caveat - the usedrange is not always the last cell that actually has data
in it. For example, if a cell below the last cell w/data has formatting
applied, the formatted cell will be the last cell in the usedrange even
though it is empty.

What could be problematic, is if the usedrange is say D5:F7 (as these are
the only cells that ever had any data in them), your code will select Row 3
(because there are only 3 rows in the usedrange). I think you would need to
change it to:

With ActiveSheet
Set rngLastRow =
..UsedRange.Cells(.UsedRange.Rows.Count,.UsedRange .Columns.Count).EntireRow
End With

but that assumes that the first caveat does not create any problems.



"ilia" wrote:

On Apr 29, 11:35 pm, kirkm
wrote:
Thanks one and all for the help and
code exapmples. Much appreciated & problem solved.
Learnt some valuable stuff too :)
Cheers - Kirk


Dim rngLastRow as Excel.Range

With ActiveSheet
Set rngLastRow
= .Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Co unt).EntireRow
End With


  #12   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Find the last used Row on a Worksheet

Hope I'm not multiposting today - darn browser keeps hanging

One caveat - the usedrange is not always the last cell that actually has
data in it. For example, if a cell below the last cell w/data has formatting
applied, the formatted cell will be the last cell in the usedrange even
though it is empty.

What could be problematic, is if the usedrange is say D5:F7 (as these are
the only cells that ever had any data in them), your code will select Row 3
(because there are only 3 rows in the usedrange). I think you would need to
change it to:

With ActiveSheet
Set rngLastRow =
..UsedRange.Cells(.UsedRange.Rows.Count,.UsedRange .Columns.Count).EntireRow
End With

but that assumes that the first caveat does not create any problems.



"ilia" wrote:

On Apr 29, 11:35 pm, kirkm
wrote:
Thanks one and all for the help and
code exapmples. Much appreciated & problem solved.
Learnt some valuable stuff too :)
Cheers - Kirk


Dim rngLastRow as Excel.Range

With ActiveSheet
Set rngLastRow
= .Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Co unt).EntireRow
End With


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
how to find and unlink current worksheet from old worksheet kmjmail Excel Discussion (Misc queries) 3 January 12th 09 10:52 PM
Find Max Value in WorkSheet Corey Excel Worksheet Functions 11 January 14th 07 11:00 PM
Find worksheet jnf40 Excel Programming 11 August 23rd 06 08:29 PM
Find worksheet cottage6 Excel Programming 8 April 21st 05 05:43 PM
find last row in another worksheet gaba Excel Programming 2 April 12th 05 04:24 AM


All times are GMT +1. The time now is 03:25 PM.

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"