0

I have a table that is:

Picture of Table

This has formulae, and the number of rows in the table can vary. I want a code in VBA which I can apply to an Active Button that would delete all the rows of the table and turn it something into like this:

Cleared Rows

However I still want the new information input in the new rows to have the same formulae applied on them as on the old information that was present before. I do this currently manually by selecting the rows and deleting them; I have even tried creating a macro but this did not work.

Any help would be appreciated :)

Edit: This is the code the Macro generated:

Sub clear3()
'
' clear3 Macro
'

'
    Range("Table3").Select
    Selection.ListObject.ListRows(1).Delete
    Selection.ListObject.ListRows(1).Delete
    Range("F11").Select
End Sub

--Now this works alright on the table when there are two rows, however when the table has 3 or 1 or just any other number of rows; I get this:

Runtime Error : '9' Subscript out of range

Алексей Р
  • 7,507
  • 2
  • 7
  • 18
  • 1
    Please edit your question and post your code attempt, and explain what did not work (error? Which line and what error) – Raymond Wu Aug 07 '21 at 16:39
  • Edited in post. Thanks so much for pointing out. – Hamza Ahmed Aug 07 '21 at 16:49
  • 1
    Just an attempt before I go to sleep, try `ActiveSheet.ListObjects("Table3").DataBodyRange.EntireRow.Delete`. If it fails, I hope someone help you! @Hamza – Raymond Wu Aug 07 '21 at 16:55
  • Are you sure you know what you want, no offence? How to "delete all rows in a table" and "I still want the new information input in the new rows to have the same formulae applied on them"? If the rows will be deleted, no formula will remain there. Please, edit your question and better explain what you try doing. You said something about doing it manually. How do you preserve the formulas when do that? Is it another meaning for "I still want the new information input in the new rows to have the same formulae applied on them" which I could not understand? – FaneDuru Aug 07 '21 at 17:35
  • 1
    Without revealing the formulas and their columns there is little to do. If the formulas depend on values in the table, you should delete only the cells containing those values in the first row, and delete the remaining entire rows (if any). If not, you have some more explaining to do. – VBasic2008 Aug 07 '21 at 18:46
  • When I say I want the formulae to remain there; I mean the following. When I input the next/new set of values, then formulae that existed on the other rows should be applied to the new set of values. Kind of like how a VLOOKUP function gets repeated on the next cell in the same column if you add a new row. I wanted those formulae to be preserved even when there are zero rows, so when I input a new row (only putting values in the first two columns), the rest of the columns should execute the same formulae they held but now based on those two values. – Hamza Ahmed Aug 08 '21 at 04:31
  • Could you simply `Clear the Contents` of the Value field. e.g. Column A. Adjust your existing formulas so that they return a null value if the Column A is empty? e.g. `=IF(A2<>"", A2, "")` You cannot delete the rows containing formulas and expect to retain the formulas. – GoodJuJu Aug 09 '21 at 09:48

1 Answers1

0
' copy this into the Worksheet code module
Private Sub CommandButton1_Click()  'ActiveX button's click event handler
    Const TABLE_NAME = "TableN" ' replace with your table name
    Dim lo As ListObject
    
    On Error Resume Next
    Set lo = Me.ListObjects(TABLE_NAME)
    If Err.Number <> 0 Then
        MsgBox TABLE_NAME & " was not found. Check the table name", vbCritical + vbOKOnly, "Sub CommandButton1_Click()"
        Exit Sub
    End If
    On Error GoTo 0
    If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
End Sub

Edit2 (multiple table cleanup)

' copy this into the Worksheet code module
Private Sub CommandButton1_Click()  'ActiveX button's click event handler
    Dim lo As ListObject, TABLE_NAME
    ' Attention! tables on the same worksheet must not have the same rows/columns,
    ' otherwise you will get an error `Run-time error '1004': This operation is not allowed.
    ' The operation is attempting to shift cells in a table on your worksheet` or something like that.
    For Each TABLE_NAME In Array("Table1", "Table2", "Table3") 'and so on
        On Error Resume Next
        Set lo = Me.ListObjects(TABLE_NAME)
        If Err.Number <> 0 Then
            MsgBox TABLE_NAME & " was not found. Check the table name", vbCritical + vbOKOnly, "Sub CommandButton1_Click()"
            Exit Sub
        End If
        On Error GoTo 0
        If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
    Next
End Sub

Edit3 (tables on the different sheets)

' copy this into the Worksheet code module
Private Sub CommandButton1_Click()  'ActiveX button's click event handler
    Dim lo As ListObject, TABLE_NAME, arr
    For Each TABLE_NAME In Array("Sheet1|Table1", "Sheet2|Table2") 'and so on
        On Error Resume Next
        arr = Split(TABLE_NAME, "|")
        Set lo = Me.Parent.Sheets(arr(0)).ListObjects(arr(1))
        If Err.Number <> 0 Then
            MsgBox TABLE_NAME & " was not found. Check the table name", vbCritical + vbOKOnly, "Sub CommandButton1_Click()"
            Exit Sub
        End If
        On Error GoTo 0
        If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
    Next
End Sub

Attention! tables on the same worksheet must not have the same rows/columns, otherwise you will get an error Run-time error '1004': This operation is not allowed. The operation is attempting to shift cells in a table on your worksheet or something like that.

An example of an acceptable table layout
enter image description here

Алексей Р
  • 7,507
  • 2
  • 7
  • 18
  • Just wanted to ask, can I copy/paste the same code twice and use it for another table in a single ActiveX button too? – Hamza Ahmed Aug 08 '21 at 05:32
  • Yes, see Edit2 option – Алексей Р Aug 08 '21 at 07:00
  • Why does it give me "Table9 was not found" error. I have the button in one sheet and table9 on another but they are in the same workbook. I am trying to clear table 9 and then paste the contents of the first two columns of another table in it. – Hamza Ahmed Aug 08 '21 at 10:11
  • If the Table9 is on the another worksheet, you must specify the sheet - see Edit3 option – Алексей Р Aug 08 '21 at 13:42
  • thanks a lot bud. I have another problem associated with the code. Please check out another question I posted on [link](https://stackoverflow.com/questions/68715672/clear-an-existing-table-then-paste-the-contents-of-another-table-into-it-vba) – Hamza Ahmed Aug 09 '21 at 16:46