gpt4 book ai didi

vba - Excel 2010、VBA 和 ListObjects 小计未随表更改而更新

转载 作者:行者123 更新时间:2023-12-02 18:46:11 35 4
gpt4 key购买 nike

因此,具有以下结构(从 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;
}
<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>

我编写了一些代码来转换 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

现在,如果您在新列的任何单元格上写入一些数字,奇怪的是 TOTAL(小计)不会更新;但如果您保存文件并重新打开它,它就会起作用并且总数将会更新。我缺少什么?

我已经尝试将 ShowTotals 移到 TotalCalculation 之后,但行为保持不变。

如果我们现在从头开始重建工作表,并在应用前面代码中的样式后添加这段代码用于 b、c 和 d 列的小计:

objLO.ListColumns("b").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("c").TotalsCalculation = xlTotalsCalculationSum
objLO.ListColumns("d").TotalsCalculation = xlTotalsCalculationSum

我注意到 b、c 和 d 的小计有效,但 Tot1、Tot2 等无效。

似乎唯一的解决方法是在添加带有创建它的引用的 ListObject 之前构造原始表。有人知道更好的解决方案吗?

提前致谢:)

最佳答案

Excel 表格中存在一个突出的错误,需要解决一些微妙之处才能获得您所需的结果。

使用显式计算技巧的粗略修复确实有效,但是虽然此方法将根据数据行中的当前值更新总计,但每次数据行中的值发生更改时都需要应用它们数据表。

有两种方法可以强制 Excel 计算总计:

  1. 您可以切换父工作表的计算状态:

    objLO.Parent.EnableCalculation = False
    objLO.Parent.EnableCalculation = True
  2. 或者,您可以替换总计公式中的 =:

    objLO.TotalsRowRange.Replace "=", "="

但是上述方法都无法为您提供持久的解决方案,使总数自动保持最新

更好的解决方案...

解决方案的线索在于,当 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

关于vba - Excel 2010、VBA 和 ListObjects 小计未随表更改而更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28840857/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com