Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Last Row Code

How could I change this part (For r = 3 To 500) of a code so that it runs to
the last row in the worksheet instead of just row 500 as it currently does.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Last Row Code

Hi MCheru

Copy the function and sub in a standard module of your workbook

Sub test()
Dim Lr As Long
Dim r As Long

Lr = LastRow(ActiveSheet)
For r = 3 To Lr
'code
Next r
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


--

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




"MCheru" wrote in message ...
How could I change this part (For r = 3 To 500) of a code so that it runs to
the last row in the worksheet instead of just row 500 as it currently does.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Last Row Code

This works very good. Thank you!

"Ron de Bruin" wrote:

Hi MCheru

Copy the function and sub in a standard module of your workbook

Sub test()
Dim Lr As Long
Dim r As Long

Lr = LastRow(ActiveSheet)
For r = 3 To Lr
'code
Next r
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


--

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




"MCheru" wrote in message ...
How could I change this part (For r = 3 To 500) of a code so that it runs to
the last row in the worksheet instead of just row 500 as it currently does.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Last Row Code

Hi Ron,


I've always used another little UDF function, I've created on my own, to
find the last row.
But I've also always never liked my own function, so when I saw yours i was
delighted, since it looked much better than mine.

But...
Your function doen't seem to find the right last row on a protected sheet?

Reason seems to be that the .Find method doesn't search protected cells.
Is it possible to make your function work on protected sheets as well, since
I really like your way of finding last row much better than mine?


TIA,


Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Last Row Code

Hi Charlotte

I not see you problem
If you want send me a test workbook private so I can look at it



--

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




"Charlotte E" <@ wrote in message ...
Hi Ron,


I've always used another little UDF function, I've created on my own, to
find the last row.
But I've also always never liked my own function, so when I saw yours i was
delighted, since it looked much better than mine.

But...
Your function doen't seem to find the right last row on a protected sheet?

Reason seems to be that the .Find method doesn't search protected cells.
Is it possible to make your function work on protected sheets as well, since
I really like your way of finding last row much better than mine?


TIA,


Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Last Row Code

No problem, Ron,


The spreadsheet is in Danish so you wouldn't understand it anyway :-)

Problem with your function is just that is doesn't find last row, if last
row is hidden and protected on a protected worksheet.
Take a look: www.EXCELGAARD.dk\LastRow.XLSm

But, I found a way around it :-)


Thanks anyway.




Ron de Bruin wrote:
Hi Charlotte

I not see you problem
If you want send me a test workbook private so I can look at it




"Charlotte E" <@ wrote in message
...
Hi Ron,


I've always used another little UDF function, I've created on my
own, to find the last row.
But I've also always never liked my own function, so when I saw
yours i was delighted, since it looked much better than mine.

But...
Your function doen't seem to find the right last row on a protected
sheet? Reason seems to be that the .Find method doesn't search protected
cells. Is it possible to make your function work on protected sheets as
well, since I really like your way of finding last row much better
than mine? TIA,


Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function



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
split post code (zip code) out of cell that includes full address Concord Excel Discussion (Misc queries) 4 October 15th 09 06:59 PM
Run VBA code only worksheet change, but don't trigger worksheet_change event based on what the code does ker_01 Excel Programming 6 October 3rd 08 09:45 PM
Shorten code to apply to all sheets except a few, instead of individually naming them, and later adding to code. Corey Excel Programming 3 December 11th 06 05:14 AM
Protect Sheet with code, but then code will not Paste error. How do i get around this. Please read for explainations.... Corey Excel Programming 4 November 25th 06 04:57 AM
Excel code convert to Access code - Concat & eliminate duplicates italia Excel Programming 1 September 12th 06 12:14 AM


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

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"