1

I have a table in the Word program like the following:

Sample Picture

By opening this table file, I have to increase or decrease the number of rows. So I can not use the formula for the sum of the seasons (Because cell numbers change). In your opinion, how can I sum up the values of "Total Season 1" and "Total Season 2" and put it in "Total Seasons"? Do you think Can I get help of the words "Total Season" or choosing the Bold words with VBA?

  • 2
    Think about to convert this common Word table to Excel table. – Akina Nov 12 '18 at 6:54
  • Unfortunately I can not run Excel. Do you think the words can not be used? – First Last Nov 12 '18 at 6:58
  • Word is not designed for complex calculations, probably it can't calculate it for you using standard calculations. VBA can be used, however there are also limitations with VBA in Word tables, it's not really straightforward. Asking for code is generally off-topic here as it sounds we need to do your job, it's acceptable only if you demonstrate your own effort to solve the problem (= post code) and ask specific question on how to fix it. – Máté Juhász Nov 12 '18 at 7:13
  • 1
    I can not run Excel. You do NOT need in Excel. Look Word, Insert pane, Tables tab. Table knob is clickable multi-variant listbox, and one of possible variants is inserting Excel table. – Akina Nov 12 '18 at 8:08
  • 1
    Quick fix would be to split your table for each season so they are separate - you can make them seem almost same by setting the paragraph spacing for the gap between them to exactly 1pt. In your total cell for each season, select the cell contents (presumably a SUM ABOVE field) and create a bookmark eg bkSeason1Total. Then in your total seasons cell, you can insert a formula field that adds up the 2 bookmarks. – Tanya Nov 13 '18 at 21:39
1

To make a table in Word perform some automatic summation of values there are formatting steps you have to take. Below is an example of what you might want the final result to look like.

enter image description here

The shaded values in the image below are created from formula fields. In order for the "=SUM(ABOVE)" formula to work in the Subtotal rows, there must be a blank row or a non-numeric cell between the parts of the table. The red arrows point to the inserted blank rows. I have made them very narrow so they look like just a line separator, but in fact they are actually a blank row.

In order to calculate the Grand Total, which is a sum of the two Subtotals, each Subtotal formula is enclosed within a bookmark. For this example they are named Materials and Services.

enter image description here

The field formulas are shown in the image below. They include the code needed for formatting.

enter image description here

To update the table after adding new data rows, you select the table and then press F9.

enter image description here

  • Hi dear friend @Rich Michaels! thank you. I have read your article. But it seems a bit long way. In the answer section, I put a code that does not need to add an empty line and the bookmarks. Good luck. – First Last Nov 17 '18 at 16:22
1

Hello dear friends who tried to help me! I worked on this topic the same day and I managed to solve the problem by writing the following code. I put this code here for everyone. If friends are willing, they can provide better codes for easier and faster running. be happy...

Sub CalculatingTotalSeasons()
ActiveDocument.Tables(1).Select
Selection.Copy
    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
    Selection.PasteAndFormat (wdFormatOriginalFormatting)

    Dim t As Table
    Dim r As Row
    Dim ts As String
    Set t = ActiveDocument.Tables(2)

    ts = "Total Season"
    For Each r In t.Rows
        If Left(r.Cells(1).Range.Text, 12) <> ts Then r.Delete
    Next r

With ActiveDocument.Tables(2)
    ActiveDocument.Tables(2).Cell(1, 1).Select
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Total Seasons"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCell
    Selection.InsertFormula Formula:="=SUM(ABOVE)", NumberFormat:=""
    Selection.MoveLeft Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.Copy
    ActiveDocument.Tables(2).Delete
End With

ActiveDocument.Tables(1).Select
With ActiveDocument.Tables(1)
    ActiveDocument.Tables(1).Cell(1, 1).Select
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Total Seasons"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCell
    Selection.PasteAndFormat (wdFormatPlainText)
End With

End Sub

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.