VBA to clear table not working properly

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
This code works fine when my table has three fields (coulumns). But if I try to use it on a table with one column it does not. The entire table disappears.

How do I change this to work on a table that only has one field?

Code:
Sub ClearUserList()

'Clear table - remove all rows except the first row
    Application.ScreenUpdating = False

    ActiveSheet.ListObjects("UserDefinedList").HeaderRowRange.Select
    'Remove the filters if one exists.
    If ActiveSheet.FilterMode Then
    Selection.AutoFilter
    End If
    'Clear all lines but the first one in the table leaving formulas for the next go round.
    With Worksheets("User List").ListObjects("UserDefinedList")
    .Range.AutoFilter
    On Error Resume Next
    .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
    .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
    ActiveWindow.SmallScroll Down:=-10000

    End With
    
    Application.ScreenUpdating = True

    Sheet1.Range("A21").Activate
    
End Sub

thanks for the help!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
If you apply SpecialCells to one cell (which is what you would be doing if your table only has one column), it will actually apply to the entire sheet. You could use something like:

Code:
If .Listcolumns.count > 1 then
.DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
else
with .databodyrange.cells(1)
   if not .hasformula then .clearcontents
end with
end if
 
Upvote 0
To clarify: How do I use this on a table that has only one column?
 
Upvote 0
Replace the line .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents in your code with what I posted.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top