Help Deleting List Objects List Columns

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Hi,

I have some code that will go through a table and delete the fields that I have deselected on a different tab. The part where I am having difficulty is the ListObject.ListColumns(#).Delete part. The problem is that the # changes, but I haven't put it in my code correctly to dynamically change (I copied what was recorded while I did it as this is the first time I'm deleting fields via VBA from a table).

How can I get the # to update to the correct column #.

As an example, I might have the field "NAME" in column C, my code says
Code:
Range("Table[[#Headers],[" & tfield & "]]").Select
Selection.ListObject.ListColumns(1).Delete

And I have other code before this which identifies tfield = "NAME"

The issue that I have realized is that I have successfully selected the field I want to delete, but then specify that I want to delete the first column (ListColumns(1)) even though this is the 3rd column in.

So how can I say something along the lines of:
Delete whatever the listcolumn is of the current selection?

Thanks in advance!

Please let me know if you have any questions.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Why don't you just use the name of the column(s) you want to delete.?
Code:
Set lst = ActiveSheet.ListObjects(1)

' delete 'NAME' column

lst.ListColumns("NAME").Delete
If you have a list fof columns you want to delete you can loop through them.
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,914
Members
449,054
Latest member
luca142

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