Search code examples
excelvbawhile-loopdifference

How to exit a Do While Loop when ColumnDifferences method no longer finds a difference?


I created a macro that searches for any numbered value change in column K using ColumnDifferences, and then inserts a formatted row as a divider between those values. There are no blanks cells between any of the numberd values. Everything works as desired with the exception of the Run-time error '1004' "No cells were found" once the ColumnDifferences method runs out of values to compare. I'm new to VBA, and programing in general. I'm sure there is some simple line of code that I'm missing to exit this loop once ColumnDifferences runs its course. Any assistance would be greatly appreciated.

Sub Divider_On_Column_Difference()
'
Range("K1").Select

Do

Range(ActiveCell, ActiveCell.End(xlDown)).Select
Selection.ColumnDifferences(ActiveCell).Select
ActiveCell.Select

    Selection.EntireRow.Insert
    Range(ActiveCell.Offset(0, -10), ActiveCell.Offset(0, -1)).Select
    Selection.RowHeight = 4
    
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = 1
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = 1
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = 1
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = 1
    End With
    ExecuteExcel4Macro "PATTERNS(1,0,5,TRUE,2,4,0,0)"
    
    ActiveCell.Offset(1, 10).Select
    
Loop While ActiveCell.Value <> ""

End Sub

Solution

  • The code has been revised and simplified.

    While it's worth noting that using Select is generally not recommended, but it is necessary before applying PATTERNS.

    Option Explicit
    Sub Divider_On_Column_Difference()
        Dim curCell As Range, endCell As Range
        Dim c As Range
        Set curCell = Range("K1")
        Set endCell = Cells(Rows.Count, "K").End(xlUp)
        Do
            If curCell = endCell Then Exit Do
            Set c = Range(curCell, endCell).ColumnDifferences(curCell)
            Set curCell = c.Cells(1)
            curCell.EntireRow.Insert
            With Cells(curCell.Row - 1, 1).Resize(1, 10)
                .RowHeight = 4
                With .Borders
                    .LineStyle = xlContinuous
                    .Weight = xlMedium
                    .ColorIndex = 1
                End With
                .Select
                ExecuteExcel4Macro "PATTERNS(1,0,5,TRUE,2,4,0,0)"
            End With
        Loop While True
    End Sub
    

    Examine the values in column K in reverse order without utilizing ColumnDifferences.

    Sub Divider_On_Column_Difference2()
        Dim endCell As Range
        Dim c As Range, i As Long
        Set endCell = Cells(Rows.Count, "K").End(xlUp)
        For i = endCell.Row To 2 Step -1
            If Cells(i, "K") <> Cells(i - 1, "K") Then
                Rows(i).Insert
                With Cells(i, 1).Resize(1, 10)
                    .RowHeight = 4
                    With .Borders
                        .LineStyle = xlContinuous
                        .Weight = xlMedium
                        .ColorIndex = 1
                    End With
                    .Select
                    ExecuteExcel4Macro "PATTERNS(1,0,5,TRUE,2,4,0,0)"
                End With
            End If
        Next
    End Sub