Consulting

Results 1 to 11 of 11

Thread: Solved: Delete Unused rows in a table

  1. #1

    Solved: Delete Unused rows in a table

    Hello,

    I have this table and I want to delete all the table rows that are empty. The problem comes in when I am trying to delete the "Table Row" and not the excel entire row. There is data next to my table and I don't wanna mess it up.


    This is the code I managed to put togeter

    [vba]Sub Delete_Empty_Rows_Table()
    Dim i As Variant
    Dim varWorkbook As Workbook
    For Each i In varWorkbook
    Set i = ListObjects.ListRows.Count
    If Range("B7:B10000").Value = "" Then
    Selection.ListObject.ListRows(i + 1).Delete
    End If
    Next i
    End Sub[/vba]
    Feedback is the best way for me to learn


    Follow the Armies

  2. #2
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Try this:

    [vba]
    Sub Delete_Empty_Rows_Table()
    Dim blankRng As Range
    On Error Resume Next
    Set blankRng = Range("B7:B10000").SpecialCells(xlCellTypeBlanks)
    blankRng.Delete shift:=xlUp
    On Error GoTo 0
    End Sub
    [/vba]

  3. #3
    Thanks for the help Ovp,

    It worked the first time I used it then it does not work any more. I have tried several reasons and I don't know whats going on
    Feedback is the best way for me to learn


    Follow the Armies

  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by fredlo2010
    Thanks for the help Ovp,

    It worked the first time I used it then it does not work any more. I have tried several reasons and I don't know whats going on
    Can you post an example of your workbook?

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    The SpecialCells method requires a fresh save to work properly each time.

  6. #6
    Quote Originally Posted by Opv
    Can you post an example of your workbook?

    here is the sample of the workbook

    Delete Empty rows.xlsx

    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  7. #7
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by fredlo2010
    here is the sample of the workbook

    Delete Empty rows.xlsx

    Thanks
    Following Kenneth's note, try this amended version, which saves the workbook automatically:

    [vba]
    Sub Delete_Empty_Rows_Table()
    Dim blankRng As Range
    On Error Resume Next
    Set blankRng = Range("B7:B10000").SpecialCells(xlCellTypeBlanks)
    blankRng.Delete shift:=xlUp
    On Error GoTo 0
    ThisWorkbook.Save
    End Sub
    [/vba]
    Last edited by Opv; 05-25-2012 at 06:46 PM.

  8. #8
    Ovp,

    I think you forgot to put code in the wrap tags. I am not sure about the idea of the saving. As a matter of fact saving the working book with a specific name an in a specific directory is something I am working on right now. But i am not sure.
    Feedback is the best way for me to learn


    Follow the Armies

  9. #9
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by fredlo2010
    Ovp,

    I think you forgot to put code in the wrap tags. I am not sure about the idea of the saving. As a matter of fact saving the working book with a specific name an in a specific directory is something I am working on right now. But i am not sure.
    The code is there now. I had to edit it...pasted the wrong code in the first time. Sorry about that.

  10. #10
    Hello,

    I got some updates on this and I wanted to share them with you guys.

    Thanks to the help from jindon from this forum I have this code now:

    [VBA]Sub Cleanup()

    WaitCleanupForm.Show

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim i As Long
    For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Cells(i, 2).Value = "" Then
    Select Case Cells(i, 1).Value
    Case "Cookies", "Salt", "Pepper", ""
    Case Else
    Cells(i, 1).Resize(1).Delete xlShiftUp
    End Select
    End If
    Next


    'reset print area

    If Range("A35") = "" Then

    ActiveSheet.PageSetup.PrintArea = Range("A1:N34").Address
    Else
    ActiveSheet.PageSetup.PrintArea = Range("A1:N1", Range("A" & Rows.Count).End(xlUp)).Address
    End If

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    WaitCleanupForm.Hide

    End Sub
    [/VBA]

    The code works fine the only issue is that it takes quite some time to run. I had to create a userform to inform that hold on this has not crashed on you this is just a slow macro.

    Maybe you guys can suggest something to speed it up a little.
    Feedback is the best way for me to learn


    Follow the Armies

  11. #11
    Hi,

    Ok nows its all done. I figured it out. First I forgot to set my
    WaitCleanupForm ShowModal to false, so the form was pretty much there forever. I also added some code to set the calculations to manual and then back to automatic. ( this helped a lot)

    so here is my code now

    [VBA]Sub Cleanup()

    WaitCleanupForm.Show
    WaitCleanupForm.Repaint

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual


    Dim i As Long
    For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Cells(i, 2).Value = "" Then
    Select Case Cells(i, 1).Value
    Case "Cookies", "Salt", "Pepper", ""
    Case Else
    Cells(i, 1).Resize(1).Delete xlShiftUp
    End Select
    End If
    Next


    'reset print area


    If Range("A35") = "" Then

    ActiveSheet.PageSetup.PrintArea = Range("A1:N34").Address
    Else
    ActiveSheet.PageSetup.PrintArea = Range("A1:N1", Range("A" & Rows.Count).End(xlUp)).Address
    End If

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    WaitCleanupForm.Hide

    End Sub [/VBA]

    Oh I also had to repaint the form because the screenupdating set to flase was making it show up blank

    Thank you so much for the help
    This thread has been solved

    The original thread can be found here
    Feedback is the best way for me to learn


    Follow the Armies

Posting Permissions

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