Add/Modify/delete calculated column formula in Excel Listobject/Table via VBA

38,737

Solution 1

Thanks to Doug's and bonCodigos comments/answers, I found the simple answer:

ListObject.ListColumns("Column name").DataBodyRange.FormulaR1C1 = "new formula"

This will overwrite any manual value (just as the normal behavior with AutoCorrect).

Solution 2

Best would be if you could show us a screen shot of your sheet. Based on that we would have articulated the answer.

Here is with the general assumption. That you have one list object to dump some data to a column and rest of the cells in that column are manually interacted.

You could try the following manually first and see if it works for you. Still the manual one is being overtaken by the code level, then you do this in code level. :)

The main action here is to Stop automatic corrections

Go To -> Tools menu -> Click AutoCorrect Options -> In the AutoCorrect Tab ->

1- To prevent ALL automatic corrections

Clear the check box for Replace Text as you type

2- To prevent SPECIFIC corrections

Clear the corresponding check box for the option. I believe you are more interested in the latter - specific data range that you just dump via a listobject.


Now here is the code for disabling this feature at code level.

When working with Excel Tables (ListObjects) there are two AutoCorrect options to consider: You can read about those two in details.

* Apply new rows and columns in table 
  (VBA AutoCorrect.AutoExpandListRange Property)
* Fill formulas in tables to create calculated columns 
 (VBA AutoCorrect.AutoFillFormulasInLists Property) 

Code you may want to use at the top of our listobject is,

Application.AutoCorrect.AutoFillFormulasInLists = False

And totally agree that it would be so much more useful if Application.AutoCorrect. AutoFillFormulasInLists controlled on a table by table basis at the ListObject level. So here is a workaround.

So one way is to clear your table data each time. And when you clear data you can make sure for TABLE TO FORGET forumulae and formatting. So it will clear the contents of the data body range before deleting table contents.

Sub forgetMe()
   With Sheet1.ListObjects("myTable")
      If Not .DataBodyRange Is Nothing Then
         .DataBodyRange.ClearContents
         .DataBodyRange.Delete
      End If
   End With

End Sub

When you input the data, start with auto stuff off.

Share:
38,737
Peter Albert
Author by

Peter Albert

Consultant, trainer and developer, specialized in Excel, Access, SQL Contact me at [email protected]

Updated on July 31, 2020

Comments

  • Peter Albert
    Peter Albert almost 4 years

    If I manually enter a formula into a column in an Excel table (i.e. ListObject), AutoCorrect applies this formula to the whole column.

    Is there any way to control the this behavior via VBA, i.e. can I somehow modify/delete/add this formula?

    I know I can simply change the formula of the ListObject.ListColumns(1).DataBodyRange object - but this will overwrite any manually values entered before - while changing the formula in the UI will leave this untouched...

  • William
    William about 8 years
    Will this also set the default formula so that when new rows are added, the formula is added as well?
  • Peter Albert
    Peter Albert about 8 years
    @William - yes, it will
  • MattE
    MattE about 5 years
    I am suddenly getting an "Invalid Use of Property" when using this...any tips?
  • Peter Albert
    Peter Albert about 5 years
    @MattE are you sure the column name is correct? Also, does your table have data - if not, DataBodyRange will be Nothing and you'll get this error.
  • MattE
    MattE about 5 years
    @PeterAlbert I think this may be the reason its happening as I am pulling in data from a query...what's the best way to wait for the query to finish loading before running this to ensure there is data there?