How To Delete Rows In VBA Based On Criteria

In this post I am going to show you all how to delete rows with the length of the value in column “A” less than 2.

The first way is the not VBA way, and it will be first to determine the length of the cell value using the “len” function:

After, you get all the resulting numbers of length equal to or less than 2,

…now you can just highlight the rows and press the “delete” key on your keyboard.

Way number 2 is to use VBA.

We will loop all the cells in column “A”, and if the length of the value is equal or less than 2, we’ll delete the row.

Here’s the code:

Sub DeleteRows()
    Dim strCellValue As String
    Dim intRow As Integer
    
    For intRow = 1 To 14487
        strCellValue = Range("A" & intRow)
        intlength = Len(strCellValue)
        
        If intlength <= 2 Then
            Rows(intRow & ":" & intRow).Select
            Selection.Delete Shift:=xlUp

        End If
    Next
    
End Sub

Way #2 seems quicker to me, what do you think?

Watch how it’s done:

****************************************************


Posted

in

by

Tags: