5

Is there anyway to check in VBA if a specific column of a table (ListObject) in Excel is a calculated column (as in http://office.microsoft.com/en-us/excel-help/use-calculated-columns-in-an-excel-table-HA010342380.aspx)?

Note that calculated columns will not only have the same R1C1 formula set for each row, but will also auto-expand when new rows are added (it will even repopulate if you delete the entire data body range and then recreate some new rows). So checking for columns with consistent formulas is not the same as checking for a calculated formula.

It's even possible for a column to be calculated, but to have one of the rows overwritten with some other formula or value, and have the auto-expand features persist.

So I'm pretty convinced this has to be some property of the column, I'm just not sure where I can access it through VBA. If it's not exposed through the VBA object model, is there some workarround to get this info?

Thanks in advance, Carlos

EDIT: I did some digging on the Excel Office Open XML files, and it turns out that what I'm looking for is the <calculatedColumnFormula> element on the <tableColumn> definition of the xl\tables\table*.xml files. Is there any way to get to that through VBA?

EDIT2: Here is an example file with the test cases that I could come up with. The VBA should signal that columns 1, 2 and 3 are calculated columns, and columns 4 and 5 aren't.

2

2 Answers 2

5

Haven't looked at this before but it appears to be a property of the ListObject's column range as follows:

Dim wks As Worksheet
Set wks = ActiveSheet
Dim li As ListObject
Set li = wks.ListObjects(1)
Dim col As ListColumn
Set col = li.ListColumns(2)    ' assuming column 2 of the table has a calculated formula
Dim r As Range
Set r = col.DataBodyRange
Let b = Not IsNull(r.FormulaArray)
if b then
    Let b = Len(r.FormulaArray) > 0 ' case where r.FormulaArray = "", suspect it's not a calculated column
End If
MsgBox b

if IsNull(r.FormulaArray) then it doesn't have a calculated column, otherwise then it does.

hth


Ok, played with this a bit and what I see is that the range object obtained using the above is different from the range object for any given cell, so if you have a given cell, I think you will need to obtain the corresponding ListColumn's range via .DataBodyRange.

(For example, if you insert Set r = r.Cells(1,1) to the above, then then the IsNull(r.FormulaArray) test no longer works to test for calculated column, but instead just says if the range has a formula, but that could be calcuated or not.)

Also, while the r.FormulaArray appears to be a string when the column is calculated, if it isn't (a calculated column), then .FormulaArray yields null, which isn't a valid string value (making it hard to use in that to capture the value you have to use a variant not a boolean); I found that IsNull(r.FormulaArray) seems to work ok.


If I add a column to the right of an already calculated column, then r.FormulaArray = "" for that newly added column. If you put a value into one of it's cells, the formula array immediately reverts to the more expected NULL. So, I added a test for what I consider this false positive.

4
  • Your code actually tests for consistent formulas within a table, which is not the same as calculated columns. I don't have access to any storage services right now, but as soon as I have I'll attach to this question an excel file showing where the code fails and containing the test cases that the solution should match. Jan 9, 2013 at 21:42
  • Ok. I'm using Excel 2010 and this code appears to effectively differentiate between a column that is calculated (as described by the article you cite) and one that either a column that has all consistent formulas or one inconsistent formulas (green triangle) or just values. I did find one example where it gave a false positive, so have added an if to test for that.
    – Erik Eidt
    Jan 9, 2013 at 22:48
  • On my test case, I am getting the behavior of table expansion as described in the article, and it correlates with the results of the above (the calculated columns are the ones whose formula is automatically filled in when the table is expanded by a row, and the non-calculated ones don't; the code above yields true for the former set and false for the latter set).
    – Erik Eidt
    Jan 9, 2013 at 22:59
  • I've posted the example on the original question. As you can see, your code throws two false negatives for columns 2 and 3, and a false positive for column 4 (just expand the table one row down and you'll see what I mean). Jan 10, 2013 at 13:49
2

This one gives appropriate answers for your example.

Unfortunately, it has a few potentially fatal drawbacks, depending one's situation. For one, it causes a recalc, so the random numbers generated via =RAND() formulas in your sample are changed.

The second drawback is that it modifies the worksheet in order to obtain the answers (it removes the modification that it makes, but still it gets modified). I can think of a few only partially helpful workarounds: (a) do this operation as seldom as needed and cache results for all columns, and (b) copy the table to a new workbook and run the routine (and delete the new workbook). While the latter would avoid the modification drawback, it still triggers a recalc for the original workbook (and otherwise has its own drawbacks). To add to that, copying the table to a new workbook looses the table/ListObject unless you copy the whole range (not just headers); then it also appears to promote the 4th column (non-calc consistent formulas) to being a calculated one. Sadly, this promotion also happens when copying the whole sheet.

Well, FWIW:

Sub TestTable()
    Dim ans As String
    Let ans = ""

    Dim li As ListObject
    Set li = ActiveSheet.ListObjects(1)

    Dim rowCountBefore As Long
    Let rowCountBefore = li.ListRows.Count

    Dim lr As ListRow
    Set lr = Nothing

    On Error Resume Next
    Set lr = li.ListRows.Add(AlwaysInsert:=True)
    On Error GoTo 0

    Dim rowCountAfter As Long
    Let rowCountAfter = li.ListRows.Count

    If Not (lr Is Nothing) And rowCountAfter = rowCountBefore + 1 Then
        Dim c As Long
        For c = 1 To li.DataBodyRange.Columns.Count
            Dim b As Boolean
            Let b = lr.Range.Cells(1, c).HasFormula
            ans = ans & "col " & c & ": " & b & ";  "
        Next
        li.ListRows(rowCountAfter).Delete
    End If

    MsgBox ans
End Sub
1
  • Thanks for the good work Erik. I was hoping for a more elegant solution, but I'm now convinced this is going to be as good as it gets. Jan 11, 2013 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.