Consulting

Results 1 to 8 of 8

Thread: Solved: Delete rows if cells in multiple columns are blank

  1. #1

    Solved: Delete rows if cells in multiple columns are blank

    Hi,

    I want to be able to delete rows in a table of 3-4000 rows where cells in columns K to AO are blank. Is there a way to do this without applying autofilter to each of those columns?

    There are plenty of solutions for single column criteria, but I can't find anything for multiple columns.

    Thanks.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Look at SpecialCells to select the ranges to delete
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    I did, and also worksheet functions. My difficulty has been combining some way of looping through each row with data, and then deleting the row if the cells in columns K:AO are blank.

  4. #4
    VBAX Regular
    Joined
    Nov 2008
    Posts
    25
    Location
    Why don't u put a new formula in column AP that concatenates cols K:ao.
    Then filter by column ap for blanks
    Then delete the blank rows

  5. #5
    I could use a formula, but would prefer something simpler. I want a short piece of code that will be run monthly. The range of columns will change with the month, but I'll cross that bridge later (probably easy enough).

    This is what I was just experimenting with.

    [VBA]Dim rw As Range
    For Each rw In Selection.Rows
    If Application.WorksheetFunction.Sum(Range("K:AO")) = 0 Then
    rw.EntireRow.Delete
    End If
    Next rw[/VBA]

    It deleted a couple of thousand rows out of 4500, but I'm not sure why, probably related to how it looped through everything. It certainly wasn't based on the criteria I want.

    I basically want to combine a test of contigous cells in a row with a looping routine that runs through each row. I've not been able to make that combination using SpecialCells either. My guess is how that range is referenced each time.

  6. #6
    It was deleting every other row. Same thing happens with this variation.

    [VBA]Dim C As Range
    Dim X, i As Integer
    X = ActiveCell.Column
    For i = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
    Set C = Cells(i, X)
    If Application.WorksheetFunction.Sum(Range("K" & i, "AO" & i)) = 0 Then
    C.EntireRow.Delete
    End If
    Next i[/VBA]

    I'm thinking you can't test a fixed range of cells in each looped row.

  7. #7
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Ron
    Windermere, FL

  8. #8
    Thanks, will take a look. I've used a solution that involves a formula, as suggested by tilamok. It seems to work.
    [VBA]Dim i As Integer
    Dim Last As Long
    Last = Cells(Rows.Count, "F").End(xlUp).Row
    Range("AP2").Value = "=COUNTA(K2:AO2)"
    Range("AP2").AutoFill Destination:=Range("AP2:AP" & Last)
    For i = Last To 2 Step -1
    If (Cells(i, "AP").Value) = "0" Then
    Cells(i, "AP").EntireRow.Delete
    End If
    Next i[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •