如何读取没有任何数据行的Excel表/ ListObject中的计算列公式

问题描述:

我有一个ListObject与外部查询作为数据源,它返回18列。 ListObject之前已经添加了另外4个计算列。如何读取没有任何数据行的Excel表/ ListObject中的计算列公式

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

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

有没有办法确定什么计算列公式是没有添加任何数据到列表对象?

这是一个解决方法,无论表格是否有行都可以使用。

getListColumnFormulae - 添加一行到表 - 填满一个1个维基座1个阵列与式为所有ListColumns - 删除行 - 返回阵列

enter image description here


enter image description here


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 
+0

与我原来的解决方法类似,“如果我手动在其中一个未计算的列中输入数据”。显然,公式存在于某处,但似乎对象模型并未公开它。 – ThunderFrame

+1

@ThunderFrame请注意,与您原始的workaround_相比,此答案的方法有一个关键区别,并且事实上,此方法不需要任何有关“ListObject”字段(“LisColumns.Items” )是_non-calculated_和哪个Fields _'HasFormula'_,因为它本身不会添加任何“数据”,而只是向DataBodyRange添加一行,以便生成“LisColumns.Items”的_“hidden”_公式“可见。 – EEM