Home » excel » excel – Want to delete entire column if it has <30 rows

excel – Want to delete entire column if it has <30 rows

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have multiple columns and multiple rows (>200 rows). i would like to delete entire column if any of the column has <30 rows data. Please help

Sub Delete_EntireColumn()
Dim iCntr
    For iCntr = 1 To 10 Step 1
        Columns(1).EntireColumn.Delete
    Next
End Sub
How to&Answers:

Try

Sub Delete_EntireColumn()
Dim LCol as Long, LRow as Long, i as Long

With Workbooks(REF).Sheets(REF)

    LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For i = LCol To 1 Step - 1
        LRow = .Cells(.Rows.Count, i).End(xlUp).Row
        If LRow < 30 Then
            .Columns(i).EntireColumn.Delete
        End If
    Next i

End With

End Sub

EDIT

In case the rows are not consecutively filled:

Sub Delete_EntireColumn()
Dim LCol as Long, LRow as Long, i as Long

With Workbooks(REF).Sheets(REF)

    LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For i = LCol To 1 Step - 1
        LRow = Application.WorksheetFunction.CountA(.Columns(i))
        If LRow < 30 Then
            .Columns(i).EntireColumn.Delete
        End If
    Next i

End With

End Sub