#1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 208
Default Lastrow

I'm familiar with this code to find the last row:

LastRow = .Range("A" & Rows.Count).End(xlUp).Row

But what if I need LastRow to be at a certain value? If I know column A is
going to have data and I want to assign LastRow the first row that has a
value of 0 how would I do that? What if I need LastRow to be the row 2 rows
above the first row that has a value of 0?
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Lastrow

I would search for the 0 and then use that (subtract 2 or whatever):

Option Explicit
Sub testme()

Dim myRow As Long
Dim FoundCell As Range
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

myRow = 0 'can't happen in real life!
With wks
With .Range("a1").EntireColumn
Set FoundCell = .Cells.Find(what:=0, _
after:=.Cells(.Cells.Count), _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
End With
End With

If FoundCell Is Nothing Then
MsgBox "not found!"
Else
If FoundCell.Row 2 Then
myRow = FoundCell.Row - 2
End If
End If

MsgBox myRow

End Sub

The .find looks for 0 after the last cell in column A and for the Next
occurrence--equivalent of looking at the top to the bottom.

If I wanted to find the last 0 in the range, I'd use:

With .Range("a1").EntireColumn
Set FoundCell = .Cells.Find(what:=0, _
after:=.Cells(1), _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=False)
End With

This starts at the first cell and looks for the previous (up the range)
occurrence.

Bishop wrote:

I'm familiar with this code to find the last row:

LastRow = .Range("A" & Rows.Count).End(xlUp).Row

But what if I need LastRow to be at a certain value? If I know column A is
going to have data and I want to assign LastRow the first row that has a
value of 0 how would I do that? What if I need LastRow to be the row 2 rows
above the first row that has a value of 0?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11,058
Default Lastrow

LastRow is just the name of a variable that can be assigned any value:

LastRow=1
or
LastRow=5+7

The formula you posted just assigns it to the last filled cell in some
column. To set LastRow to the first cell in column A that has the value of
zero:

Sub marine()
For i = 1 To Rows.Count
If Cells(i, "A").Value = 0 Then
LastRow = i
Exit For
End If
Next
' more code here
End Sub

--
Gary''s Student - gsnu200852


"Bishop" wrote:

I'm familiar with this code to find the last row:

LastRow = .Range("A" & Rows.Count).End(xlUp).Row

But what if I need LastRow to be at a certain value? If I know column A is
going to have data and I want to assign LastRow the first row that has a
value of 0 how would I do that? What if I need LastRow to be the row 2 rows
above the first row that has a value of 0?

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
Lastrow Mr. Damon[_2_] Excel Worksheet Functions 3 July 30th 08 04:12 PM
HELP Lastrow Range Mr. Damon Excel Discussion (Misc queries) 1 July 29th 08 01:21 PM
LastRow of Data phmckeever Excel Worksheet Functions 1 December 1st 06 09:04 PM
Type mismatch on LastRow daniel chen Excel Discussion (Misc queries) 8 April 11th 05 02:29 AM
Lastrow in Range Jeff Excel Discussion (Misc queries) 2 December 17th 04 04:53 PM


All times are GMT +1. The time now is 10: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"