Find 1st non matching cell with vba

Viewed 44

I need to find 1st non matching cell in a column. For example, values of cell A1 to A7 is "apple" then "mango" for next few cells in the same column and so on. I am using this code:

item1 = activecell.value
do while activecell.value = item1
    doevents
    cells(activecell.row + 1, 1).select
loop

This code is working fine. But I want to replace it with a faster code. Please suggest something.

1 Answers

1st Non-Matching Cell in Column

  • Here's a dirty solution that might be of help.

The Code

Sub SelectNextDifferentCellInColumn()
    
    Dim cel As Range: Set cel = ActiveCell
    Dim Curr As Variant: Curr = cel.Value
    
    Dim FirstEmptyRow As Long
    FirstEmptyRow = Cells(Rows.Count, _
                          Columns(cel.Column).Column).End(xlUp).Offset(1).Row
    
    On Error Resume Next
    Do
        Set cel = cel.Offset(1)
        If cel.Row > FirstEmptyRow Then Exit Sub
    Loop Until Curr <> cel.Value
    
    cel.Select

End Sub
Related