Find Row Of Last Instance of A Value (A Break In The Series) From The Top Of A Column

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have Googled and I have searched this forum, but my results are only as good as the criteria I used to search.
I found a lot of references to similar concepts, but nothing exclusiver to the uniqueness of my quest.

Is anyone able to suggest a VBA solution to find the row number with the last instance of the number 1 from the top of column W of my worksheet?


Book1
W
11
21
30
40
50
60
71
80
90
100
111
121
131
140
150
160
170
Sheet2


In this case, I'm looking to find a row number of 2.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Ark68,

Try this:

Code:
Option Explicit
Sub Macro1()

    Dim rngMyCell As Range
    Dim strMyCol As String
    Dim varMyValue As Variant
    
    Application.ScreenUpdating = False
    
    strMyCol = "W"
    varMyValue = Range(strMyCol & "1")
    
    For Each rngMyCell In Range(strMyCol & "1:" & strMyCol & Cells(Rows.Count, strMyCol).End(xlUp).Row)
        If CVar(rngMyCell.Offset(1, 0)) <> varMyValue Then
            Exit For
        End If
    Next rngMyCell
    
    Application.ScreenUpdating = True
    
    MsgBox rngMyCell.Row

End Sub

Just change the strMyCol variable to suit if required.

Regards,

Robert
 
Upvote 0
Thank you Robert. I think I may have messed up...

Rich (BB code):
        With ws_data
            strMyCol = "W"
            varMyValue = .Range(strMyCol & "1")
    
            For Each rngMyCell In .Range(strMyCol & "1:" & strMyCol & .Cells(Rows.count, strMyCol).End(xlUp).row)
                If CVar(rngMyCell.offset(1, 0)) <> varMyValue Then
                    Exit For
                End If
            Next rngMyCell
        End With
        
        Application.ScreenUpdating = True
        MsgBox rngMyCell.row

The result I am getting, is 1, which is the header row. W1 is actually empty,it doesn't have any value in it.

I changed this line to start at row 2 intead of 1.
Rich (BB code):
For Each rngMyCell In .Range(strMyCol & "2:" & strMyCol & .Cells(Rows.count, strMyCol).End(xlUp).row)

This gives me a value of 2. Cell W2 does have a value of 1 in it, but it's not the last in the series. W3 is the last cell with 1 in it, with W4 = 0.
 
Upvote 0
The cell reference the varMyValue variable was pointing to had to be changed as well i.e. varMyValue = .Range(strMyCol & "2")

Try this where both the source column and starting row are variables and so once set they are applicable for the remainder of the code:

Code:
Option Explicit
Sub Macro1()

    Dim rngMyCell As Range
    Dim strMyCol As String
    Dim varMyValue As Variant
    Dim lngStartRow As Long
    
    Application.ScreenUpdating = False
    
    strMyCol = "W"
    lngStartRow = 2
    varMyValue = Range(strMyCol & lngStartRow)
    
    For Each rngMyCell In Range(strMyCol & lngStartRow & ":" & strMyCol & Cells(Rows.Count, strMyCol).End(xlUp).Row)
        If CVar(rngMyCell.Offset(1, 0)) <> varMyValue Then
            Exit For
        End If
    Next rngMyCell
    
    Application.ScreenUpdating = True
    
    MsgBox rngMyCell.Row

End Sub

Regards,

Robert
 
Last edited:
Upvote 0
Thanks Robert for your continued support. This seems to be performing a bit better, but I can't seem to get the result.

Rich (BB code):
        With ws_data
            strMyCol = "W"
            lngStartRow = 2
            varMyValue = .Range(strMyCol & lngStartRow)
    
            For Each rngMyCell In .Range(strMyCol & lngStartRow & ":" & strMyCol & Cells(.Rows.count, strMyCol).End(xlUp).row)
                If CVar(rngMyCell.offset(1, 0)) <> varMyValue Then
                    Exit For
                End If
            Next rngMyCell
        End With
        
        Application.ScreenUpdating = True
        MsgBox rngMyCell.row

Line in red leaving me with an "Object variable or With Block variable not set"
 
Upvote 0
Ooops. Found my mistake!

Rich (BB code):
For Each rngMyCell In .Range(strMyCol & lngStartRow & ":" & strMyCol & .Cells(.Rows.count, strMyCol).End(xlUp).row)
 
Last edited:
Upvote 0
I'm glad we got it all sorted in the end :)

Thanks for the thanks and the like ;)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

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