• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to Bulk delete Table rows, based on a Table column value?

Status
Not open for further replies.

inddon

Member
Hello There,

I have a excel Table. I would like to delete bulk rows where a table column value is empty.

Attached a sample workbook. Also,the code below for a quick view.

For example in worksheet 'Final':
It should delete all rows where TableColumn 'Description' has no value. TableColumn 'Description' is based on worksheet 'RawData' Table1


Could you please advise, how this can be achieved using VBA?

Code:
Sub TableDeleteInsertRows()
Dim wsRaw As Worksheet, wsFinal As Worksheet
Dim tbl1 As ListObject, tbl3 As ListObject
Dim tbl1Count As Integer, tbl3Count As Integer

'Setting worksheet and table1
Set wsRaw = Worksheets("RawData")
Set tbl1 = wsRaw.ListObjects("Table1")

'Setting worksheet and table3
Set wsFinal = Worksheets("Final")
Set tbl3 = wsFinal.ListObjects("Table3")

'Row count for later use
tbl1Count = tbl1.DataBodyRange.Rows.Count
tbl3Count = tbl3.DataBodyRange.Rows.Count

'Store current Calculation then switch to manual.
'Turn off events and screen updating
  With Application
  xlCalc = .Calculation
  .Calculation = xlCalculationManual
  .EnableEvents = False
  .ScreenUpdating = False
  End With

  'Remove any filters
  ActiveSheet.AutoFilterMode = False


'-------------------------------------------------------
'Delete all but 1st row. Offset will adjust range down 1,
'resize is used to adjust delete range to row.count -1.
'Columns calculation is there just in case you have table
'with more than 1 column. If statement needed to avoid
'error when there is only 1 row in databodyrange
'-------------------------------------------------------
If tbl3Count > 1 Then
  tbl3.DataBodyRange.Offset(1, 0).Resize(tbl3Count - 1, _
  tbl3.DataBodyRange.Columns.Count).Rows.Delete
End If

'-------------------------------------------------------
'Instead of insert rows, resize table range. +1 to
'tbl1Count is used to include header row.
'-------------------------------------------------------
tbl3.Resize Range(Cells(1, 1), Cells(tbl1Count + 1, tbl3.DataBodyRange.Columns.Count))


'-------------------------------------------------------
' Here delete bulk all rows where TableColumn 'Description' has no value
'-------------------------------------------------------



'Revert back
With Application
  .Calculation = xlCalc
  .EnableEvents = True
  .ScreenUpdating = True
End With

Set tbl1 = Nothing
Set tbl3 = Nothing

Set wsRaw = Nothing
Set wsFinal = Nothing

End Sub

Many thanks & regards,
Don
 

Attachments

  • Sample workbook - 3.xlsm
    24.5 KB · Views: 4
Last edited:
Status
Not open for further replies.
Back
Top