问题
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