Clear Excel Table (ListObject
) Columns
Main
Sub NewHours()
'
' New_Hours Macro
' Moves down a sheet and removes previous hours
'
' Keyboard Shortcut: Ctrl+Shift+L
'
On Error GoTo ClearError
With ActiveSheet.Next
If ClearBetweenTableColumns(.ListObjects(1), "Sunday", "Saturday") Then
Application.Goto .Range("E9")
End If
End With
ProcExit:
Exit Sub
ClearError:
'Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume ProcExit
End Sub
The Method
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Clears the contents between two columns of an Excel table.
' Returns a boolean indicating whether it was successful.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ClearBetweenTableColumns( _
ByVal Table As ListObject, _
ByVal StartColumnID As Variant, _
ByVal EndColumnID As Variant) _
As Boolean
On Error GoTo ClearError
With Table
Dim sCol As Long: sCol = .ListColumns(StartColumnID).Index
Dim eCol As Long: eCol = .ListColumns(EndColumnID).Index
Dim cCount As Long: cCount = eCol - sCol + 1
.DataBodyRange.Resize(, cCount).Offset(, sCol - 1).ClearContents
End With
ClearBetweenTableColumns = True
ProcExit:
Exit Function
ClearError:
'Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume ProcExit
End Function
Surprises (Main Analized)
Sub NewHoursEDU()
If ActiveSheet Is Nothing Then Exit Sub ' no visible workbooks open
If ActiveSheet.Index = ActiveSheet.Parent.Sheets.Count Then Exit Sub ' last
Dim sh As Object: Set sh = ActiveSheet.Next
If Not TypeOf sh Is Worksheet Then Exit Sub ' not a worksheet
If sh.ListObjects.Count = 0 Then Exit Sub ' no table
If ClearBetweenTableColumns(sh.ListObjects(1), "Sunday", "Saturday") Then
Application.Goto sh.Range("E9")
'Else ' an error occurred in the called procedure; do nothing!?
End If
End Sub