Zen*_*ata 7 vba subtotal excel-2010 listobject
所以,有这个结构(从A1开始 - 显示片段>运行):
table {
border-color: #BBB;
border-width: 0px 0px 1px 1px;
border-style: dotted;
}
body {
font: 12px Arial, Tahoma, Helvetica, FreeSans, sans-serif;
color: #333;
}
td {
border-color: #BBB;
border-width: 1px 1px 0px 0px;
border-style: dotted;
padding: 3px;
}Run Code Online (Sandbox Code Playgroud)
<table>
<tbody>
<tr>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
<tr>
<td>1</td>
<td>Title 1</td>
<td>Title 2</td>
<td>Title 3</td>
<td>Title 4</td>
</tr>
<tr>
<td>2</td>
<td>GH</td>
<td>1</td>
<td>434</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>TH</td>
<td>3</td>
<td>435</td>
<td>5</td>
</tr>
<tr>
<td>4</td>
<td>TH</td>
<td>4</td>
<td>4</td>
<td>6</td>
</tr>
<tr>
<td>5</td>
<td>LH</td>
<td>2</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>6</td>
<td>EH</td>
<td>2</td>
<td>5</td>
<td>36</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
我编写了一些代码来在ListObject中转换该范围(A1:D6),添加了4个新列和小计:
Function test()
Dim objLO As ListObject
Set objLO = ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$D$6"), , xlYes)
objLO.Name = "Recap"
objLO.TableStyle = "TableStyleMedium2"
objLO.ListColumns.Add (objLO.ListColumns.Count + 1)
objLO.HeaderRowRange(objLO.ListColumns.Count) = "Tot1"
objLO.ListColumns.Add (objLO.ListColumns.Count + 1)
objLO.HeaderRowRange(objLO.ListColumns.Count) = "Tot2"
objLO.ListColumns.Add (objLO.ListColumns.Count + 1)
objLO.HeaderRowRange(objLO.ListColumns.Count) = "Tot3"
objLO.ListColumns.Add (objLO.ListColumns.Count + 1)
objLO.HeaderRowRange(objLO.ListColumns.Count) = "Tot4"
objLO.ShowTotals = True
objLO.ListColumns("Tot1").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("Tot2").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("Tot3").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("Tot4").TotalsCalculation = xlTotalsCalculationSum
End Function
Run Code Online (Sandbox Code Playgroud)
现在,如果你继续新列的任何单元格并写一些数字,奇怪的是TOTAL(小计)不会更新; 但是如果您保存文件并重新打开它,它将起作用,总计将更新.我错过了什么?
我已经尝试在TotalCalculation之后移动ShowTotals但行为保持不变.
如果我们现在从头开始重建工作表,并在应用前一代码中的样式后为列b,c和d添加这段代码:
objLO.ListColumns("b").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("c").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("d").TotalsCalculation = xlTotalsCalculationSum
Run Code Online (Sandbox Code Playgroud)
我注意到b,c和d的小计正在工作但不适用于Tot1,Tot2等.
似乎唯一的解决方法是在添加带有用于创建它的引用的ListObject之前构造原始表.谁知道更好的解决方案?
提前致谢 :)
Excel 表格中存在一个突出的错误,需要解决一些微妙之处才能获得所需的结果。
使用显式计算技巧的粗略修复确实有效,但是虽然这种方法将根据数据行中的当前值更新总计,但每次数据表中的值发生更改时都需要应用它们。
有 2 种方法可以强制 Excel 计算总计:
您可以切换父工作表的计算状态:
objLO.Parent.EnableCalculation = False
objLO.Parent.EnableCalculation = True
Run Code Online (Sandbox Code Playgroud)或者,您可以替换=总计公式中的:
objLO.TotalsRowRange.Replace "=", "="
Run Code Online (Sandbox Code Playgroud)但上述方法都没有为您提供持久的解决方案,使总数自动保持最新。
更好的解决方案...
解决方案的线索在于,当 ListObject 从范围转换为 ListObject 时,会动态计算存在的列的小计。
您可以利用这些知识,并确保不是将列附加到 ListObject 的末尾/右侧,而是将它们插入到现有列之前。但由于您最终希望新列位于最右侧,因此这种方法需要在原始范围中使用虚拟列,然后将所有新列插入到虚拟列之前,最后可以删除虚拟列。
请参阅此修改后的代码,并附有注释:
Function test()
Dim objLO As ListObject
'Expand the selection to grab an additional Dummy column
Set objLO = ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$E$6"), , xlYes)
objLO.Name = "Recap"
objLO.TableStyle = "TableStyleMedium2"
'Insert all of the new columns BEFORE the Dummy column
objLO.ListColumns.Add (objLO.ListColumns.Count)
objLO.HeaderRowRange(objLO.ListColumns.Count - 1) = "Tot1"
objLO.ListColumns.Add (objLO.ListColumns.Count)
objLO.HeaderRowRange(objLO.ListColumns.Count - 1) = "Tot2"
objLO.ListColumns.Add (objLO.ListColumns.Count)
objLO.HeaderRowRange(objLO.ListColumns.Count - 1) = "Tot3"
objLO.ListColumns.Add (objLO.ListColumns.Count)
objLO.HeaderRowRange(objLO.ListColumns.Count - 1) = "Tot4"
'Must show totals BEFORE applying totals, otherwise the last column defaults to Count (even if we override it)
objLO.ShowTotals = True
objLO.ListColumns("Tot1").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("Tot2").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("Tot3").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("Tot4").TotalsCalculation = xlTotalsCalculationSum
'Remove the extra dummy column
objLO.ListColumns(objLO.ListColumns.Count).Delete
'Now toggle the ShowTotals to force the ListObject to recognise the new column totals
objLO.ShowTotals = False
objLO.ShowTotals = True
End Function
Run Code Online (Sandbox Code Playgroud)