omagoodness
Board Regular
- Joined
- Apr 17, 2016
- Messages
- 67
I have a small table (tblTips) that collects data from a userform for managing employee tips payable. Data is entered every day for a week. I have a pivot table that filters and summarizes the data which is then automatically sent to the individual responsible for distribution of the tips to employees.
After the data from the pivot table is sent, I want the table data (source data) to clear but keep the pivot table structure in place for the next week.
This is the code I am using to clear the contents of the source data (tblTips). When I clear the source data, the pivot table gets deleted. Is there a way to prevent this?
After the data from the pivot table is sent, I want the table data (source data) to clear but keep the pivot table structure in place for the next week.
This is the code I am using to clear the contents of the source data (tblTips). When I clear the source data, the pivot table gets deleted. Is there a way to prevent this?
Code:
Sub ClearTips()
Dim ws As Worksheet, pt As PivotTable, TipTbl As ListObject
Set ws = Sheets("Tip Calculator")
Set TipTbl = ws.ListObjects("tblTips")
On Error Resume Next
'clear the period from and to dates
If ws.ProtectContents = True Then
ws.Unprotect ("**********")
ws.Range("AG4").SpecialCells(xlCellTypeConstants).ClearContents ' date cell
ws.Range("AI4").SpecialCells(xlCellTypeConstants).ClearContents ' date cell
End If
'delete the table rows
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With ThisWorkbook.Worksheets("Tip Calculator")
If .ListObjects.Count > 0 Then
With .ListObjects("tblTips")
While .ListRows.Count > 0 'Delete all rows except first
.ListRows(.ListRows.Count).Delete
Wend
End With
End If
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
ws.Protect ("************")
End Sub