Find last row - conditionally

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Google seems to have everything but !
Is there a good way to find the row number for the last item in "A" column, where item is a specific thing.
e.g. Row number for the last row where Col A = 3?

Or, if easier can you "get" a range ? e.g. all cells = "3" in Col "C" ?
I need to calculate this in Form code, not mousing on worksheet.

Thanks for any help.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Is there a good way to find the row number for the last item in "A" column, where item is a specific thing.
e.g. Row number for the last row where Col A = 3?
Give this a try..

Code:
[table="width: 500"]
[tr]
	[td]LastRowWith[B][COLOR="#FF0000"]3[/COLOR][/B]inColumn[B][COLOR="#0000FF"]A[/COLOR][/B] = Columns("[B][COLOR="#0000FF"]A[/COLOR][/B]").Find([B][COLOR="#FF0000"]3[/COLOR][/B], , xlValues, , xlRows, xlPrevious, , , False).Row[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Many thanks Rick, that works spot on! With that I thought I could figure out the next bit...but alas...

I need to get the row before that which contain s "001". Please ??
 
Upvote 0
Many thanks Rick, that works spot on! With that I thought I could figure out the next bit...but alas...

I need to get the row before that which contain s "001". Please ??
Code:
[table="width: 500"]
[tr]
	[td][B][COLOR="#0000FF"]LastRowWith3inColumnA[/COLOR][/B] = Columns("A").Find(3, , xlValues, , xlRows, xlPrevious, , , False).Row
Prior001 = Range("A1:A" & [B][COLOR="#0000FF"]LastRowWith3inColumnA[/COLOR][/B]).Find("001", , xlValues, , xlRows, xlPrevious, , , False).Row[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Rick, with code in Msg2 looking for something that doesn't exist returns error 91 "Object Variable or With Block variable not set"
Apart from trapping this error, is there any change that would avoid that?
Thanks.
 
Upvote 0
Rick, with code in Msg2 looking for something that doesn't exist returns error 91 "Object Variable or With Block variable not set"
Apart from trapping this error, is there any change that would avoid that?
You can set a Range variable to the "found" cell... if it exists, then you can retrieve its row number. Something like this...
Code:
[table="width: 500"]
[tr]
	[td]Sub Test()

  Dim LastCellWith3inColumnA As Range, Prior001 As Long, LastRowWith3inColumnA As Long
  
  Set LastCellWith3inColumnA = Columns("A").Find(3, , xlValues, , xlRows, xlPrevious, , , False)
  
  If Not LastCellWith3inColumnA Is Nothing Then
    LastRowWith3inColumnA = LastCellWith3inColumnA.Row
    Prior001 = Range("A1:A" & LastRowWith3inColumnA).Find("001", , xlValues, , xlRows, xlPrevious, , , False).Row
  End If

End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top