Consulting

Results 1 to 9 of 9

Thread: Solved: delete rows which are not completely filled

  1. #1

    Solved: delete rows which are not completely filled

    Hi all,
    Thanks once again for answering my previous threads. But here i am with another Question.

    I was wondering if I could programmically delete rows which are not fully filled to column J by using worksheet change?


    Thanks. And have a great weekend.



  2. #2
    Super Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    i suppose the easiest way would be to look for blanks in column J for then delete the entire row if it is blank, like this,

    [vba]
    Sub Remove_Row_Column_J_Blanks()
    Dim JLastRow As Long
    Dim i As Long
    Application.ScreenUpdating = False
    JLastRow = Cells(Rows.Count, "J").End(xlUp).Row'''''find used range in column j including blanks
    For i = JLastRow To 1 Step -1''''work backwards from last row
    If Cells(i, "J").Value = "" Then'''''if the cell numbered i in column J is blank then
    Rows(i).Delete'''''delete the entire row numbered i
    End If
    Next i
    Application.ScreenUpdating = True
    End Sub
    [/vba]Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Use CountBlanks in a loop to check for any empty cells, or if just for column J
    [VBA]Sub Macro1()
    Columns("J").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End Sub
    [/VBA]
    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'

  4. #4
    Hi,

    thanks for both of your replies. I've tried both and it works out fine.
    However, for mdmackillop's code, my screen kept on flashing and updating. I tried screenupdating with worksheet change but no avail. May i know how do i get rid of this?


    Thanks

  5. #5
    VBAX Newbie
    Joined
    Aug 2006
    Posts
    4
    Location
    Try

    [VBA] Sub DelEmptyRows()
    Dim i As Long, iLimit As Long
    iLimit = ActiveSheet.UsedRange.Rows.Count

    Application.Calculation = xlCalculationManual
    For i = iLimit To 1 Step -1
    If Application.CountA(Cells(i, 1).EntireRow) = 0 Then
    Cells(i, 1).EntireRow.Delete
    End If
    Next i
    Application.Calculation = xlCalculationAutomatic
    'Application.ScreenUpdating = True
    iLimit = ActiveSheet.UsedRange.Rows.Count 'attempt to fix lastcell

    End Sub[/VBA]

  6. #6
    Super Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    MNJ, at the very top of your code after you declare your variables you need this line [VBA]Application.ScreenUpdating = False[/VBA]and before your End Sub you need this [VBA]Application.ScreenUpdating = True[/VBA]Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  7. #7
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Also note that if your using worksheet change that it will fire every time you change anything on the sheet...you might want to run it as a regular macro when you call it only.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Many thanks for your replies. I've worked it out .


    ,
    MNJ

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by lucas
    Also note that if your using worksheet change that it will fire every time you change anything on the sheet...you might want to run it as a regular macro when you call it only.....
    Or as a Worksheet_Activate or Deactivate event...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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