So, having this structure (starting with A1 - show snippet> run):
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>
I wrote code to convert this range (A1: D6) to ListObject, added 4 new columns and subtotals:
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
Now, if you go to any cell of the new columns and write some numbers, it is strange that TOTAL (subtotal) is not updated; but if you save the file and close it again, it will work and the totals will be updated. What am I missing?
I already tried moving ShowTotals after TotalCalculation, but the behavior remains the same.
If we now rebuild the sheet from scratch and add this code snippet for subtotals for columns b, c and d after applying the style in the previous code:
objLO.ListColumns("b").TotalsCalculation = xlTotalsCalculationSum objLO.ListColumns("c").TotalsCalculation = xlTotalsCalculationSum objLO.ListColumns("d").TotalsCalculation = xlTotalsCalculationSum
I noticed that subtotals for b, c and d work, but not for Tot1, Tot2, etc.
It seems that the only workaround is to create the source table before adding a ListObject with links to its creation. Does anyone know a better solution?
Thank you in advance:)
source share