我有一个以外部查询作为数据源的 ListObject,它返回 18 列。 ListObject 之前添加了额外的 4 个计算列。

现在,ListObject 有 0 个数据行,但是,虽然有 0 个数据行,但我似乎无法读取计算列的预定义公式。

如果我刷新数据源,并且数据源返回至少 1 行,则计算列的公式变得可读。同样,如果我在非计算列之一中手动输入数据,以便至少有一行,则计算列公式是可读的。

有没有办法在不向列表对象添加任何数据的情况下确定计算的列公式是什么?

最佳答案

这是一个无论表是否有行都有效的解决方法。
getListColumnFormulae - 在表格中添加一行
- 用所有 ListColumns 的公式填充一维基数为 1 的数组
- 删除行
- 返回数组

vba - 如何在没有任何数据行的情况下读取 Excel Table/ListObject 中计算列的公式-LMLPHP

vba - 如何在没有任何数据行的情况下读取 Excel Table/ListObject 中计算列的公式-LMLPHP

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

关于vba - 如何在没有任何数据行的情况下读取 Excel Table/ListObject 中计算列的公式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40734024/

10-11 20:20
查看更多