1

I'm trying to clear cell contents within a specified table column range ([Front Straddle]:[Front Option]), of all tables within a specified worksheet. This script will only live within the "VolJump" worksheet, which contains an arbitrary number of identically formatted, differently named tables. Because of this, I felt the best approach was to reference the tables by the index number.

I'm running into issues with the proper referencing/nesting within the Range function below.

Sub ClearCells()

    Dim i As Long
    Dim sh As Worksheet

    Set sh = ThisWorkbook.Worksheets("VolJump")

    If sh.ListObjects.Count > 0 Then
        For i = 1 To sh.ListObjects.Count
            Range("Activesheet.ListObjects(1)[[Front Straddle]:[Front Option]]").Select
            Selection.ClearContents
        Next i
    End If

End Sub
Community
  • 1
  • 1
jlakes85
  • 57
  • 1
  • 6

2 Answers2

3

Clear Contents of Table Columns Range

Option Explicit

Sub ClearCells()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("VolJump")
    Dim tbl As ListObject
    For Each tbl In ws.ListObjects
        ws.Range(tbl.Name & "[[Front Straddle]:[Front Option]]").ClearContents
    Next tbl
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
2

Just using the ListObjects:

Sub ClearColumns()

    Dim lo As ListObject
    Dim ColNum1 As Long, ColNum2 As Long
    For Each lo In ThisWorkbook.Worksheets("Sheet1").ListObjects
        'Get the index numbers of the start and end columns.
        ColNum1 = lo.ListColumns("Front Straddle").Index
        ColNum2 = lo.ListColumns("Front Option").Index
        
        'Resize the range from the start column to the end column and clear it.
        lo.DataBodyRange.Columns(ColNum1).Resize(, ColNum2 - ColNum1 + 1).ClearContents
    Next lo

End Sub  
Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45