VBA: Delete All Columns in a Table minus the first column

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
888
Office Version
  1. 365
Platform
  1. Windows
I have tried a few codes online but I cannot find one that does what I am looking for.

I have a table called "PDF" on Sheet "PDF".
What I would like is a code that deletes every column in this table minus the first one so the table still exists and is ready to accept new data (therefor expanding accordingly)

The table starts on cell A36. I already have a code that clears all of the rows below A37 but it is the columns that are the problem as I need to keep the sheet columns as there is data above the table and the headers of the table can vary. It would be better if there was a way to use relative references and delete columns 2-12 for example but without deleting the table.

The below code unfortunately seems to delete the table

VBA Code:
 'Declare Variables
    Dim oSheetName As Worksheet
    Dim sTableName As String
    Dim loTable As ListObject
 
    'Define Variable
    sTableName = "PDF"
 
    'Define WorkSheet object
    Set oSheetName = Sheets("PDF")
 
    'Define Table Object
    Set loTable = oSheetName.ListObjects(sTableName)
 
    'Loop through to delete
    For iCnt = 2 To 12
        'Delete multiple columns from the table
        loTable.ListColumns(1).Delete
    Next

Any help with this code would be greatly appreciated!!

ALSO if there was any way to delete any column that contains the word "Column" ?? Basically if the column has no data I would like it deleted from the Table.

Thank you
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
See if loTable.DataBodyRange.Delete does what you want
 
Upvote 0
Hi, try this code
VBA Code:
Sub test()

Dim tbl As ListObject

Set tbl = ThisWorkbook.Sheets("PDF").ListObjects("PDF")

tbl.DataBodyRange.Offset(0, 1).Resize(tbl.DataBodyRange.Rows.Count, _
    tbl.DataBodyRange.Columns.Count - 1).Columns.Delete

End Sub
 
Upvote 0
Here is another macro that you can try...
VBA Code:
Sub Test()
  With Sheets("PDF").ListObjects("PDF").DataBodyRange
    Intersect(.Cells, .Offset(, 1)).ClearContents
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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