How to read formulas of Calculated Columns in an Excel Table/ListObject without any data rows

强颜欢笑 提交于 2019-12-22 08:47:39

问题


I have a ListObject with an external query as the data source, which returns 18 columns. The ListObject has previously had an additional 4 calculated columns added.

Right now, the ListObject has 0 data rows, however, while there are 0 data rows, I don't seem to be able to read the pre-defined formulas of the calculated columns.

If I refresh the data source, and the data source returns at least 1 row, then the formulas for the calculated columns become readable. Likewise, if I manually enter data in one of the non-calculated columns, so that there is at least one row, then the calculated column formulas are readable.

Is there a way to determine what the calculated column formulas are without adding any data to the list object?


回答1:


Here is a workaround that will work whether the table has rows or not.

getListColumnFormulae - Adds a row to table - Fills an 1 dimensional base 1 array with the formulas for all the ListColumns - Deletes the row - Return the array



Function getListColumnFormulae(tbl As ListObject)
    Dim Formulae
    On Error Resume Next
    With tbl.ListRows.Add
        Formulae = Application.Transpose(.Range.Formula)
        Formulae = Application.Transpose(Formulae)
        getListColumnFormulae = Formulae
        .Delete
    End With
    On Error GoTo 0
End Function

Sub FormulaeMessage()
    Dim Data
    Dim tbl As ListObject
    Set tbl = Worksheets("Sheet2").ListObjects(1)
    Data = getListColumnFormulae(tbl)

End Sub


来源:https://stackoverflow.com/questions/40734024/how-to-read-formulas-of-calculated-columns-in-an-excel-table-listobject-without

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!