giorgiline
giorgiline

Reputation: 1371

How to properly clear content of specific columns in a table (excel)

The table I'm working with has about 40k rows and the data I want to clear are in the first 15 columns, so I have code it the following to do the job:

Sub DeleteOperationsTable()
   Application.EnableEvents = False
   Dim i As Integer
   For i = 1 To 15
    (*) Sheet(1).ListObjects("Table1").ListColumns(i).DataBodyRange.Clear
   Next i
   Application.EnableEvents = True
End Sub

It happens that only the columns 1 to 7 are cleared.

So to debug the problem I've set a breakpoint (*) and it seems that it enters in an infinite loop. After the 7th iteration the sub start again from the beginning.

Can anyone tell me why this happens?

Upvotes: 1

Views: 10199

Answers (1)

Vityata
Vityata

Reputation: 43565

Let me guess! :) You have events in the worksheet, which is calling the DeleteOpertationsTable() by change of the worksheet.

If you disable the events through Application.EnableEvents = False your code should work. I have tried this and it was ok:

For i = 1 To 15
    Sheets(1).ListObjects("Table1").ListColumns(i).DataBodyRange.Clear
Next i

Upvotes: 1

Related Questions