Consulting

Results 1 to 5 of 5

Thread: Add columns in Word

  1. #1
    VBAX Newbie
    Joined
    Aug 2024
    Posts
    1
    Location

    Unhappy Add columns in Word

    Hi, I want to add the columns in a word table. I know I can use the function command but that doesn't allow for empty cells. I used to have a macro that would add up all the columns including empty cells within the table. Can anyone help please?

    Thank you so much :-)

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,217
    Location
    Maybe something like this:

    Sub AddCol()
        Dim t As Table
        Set t = ActiveDocument.Tables(1)
        t.Columns.Add
        t.Columns.Autofit
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Regular
    Joined
    Jan 2022
    Posts
    24
    Location
    Hi, KiwiAlex! Will you, please, specify your request. How many columns and which ones would you like to add. It would be nice to see the table you are working with and the places of columns to add (e.g., "add a column to the right of column 3" or "add 3 columns to the left of column 5", etc.).

  4. #4
    Banned VBAX Newbie
    Joined
    Aug 2024
    Posts
    2
    Location
    Here's a VBA macro that will add up the columns in a Word table, even if there are empty cells:
    Sub AddColumns()    
        Dim tbl As Table
        Dim rng As Range
        Dim i As Long, j As Long, k As Long
        Dim sum As Double
        Set tbl = ActiveDocument.Tables(1) ' Adjust the table number if needed
        For j = 1 To tbl.Columns.Count
            sum = 0
            For i = 1 To tbl.Rows.Count
                Set rng = tbl.Cell(i, j).Range
                If rng.Text <> "" Then
                    sum = sum + Val(rng.Text)
                End If
            Next i
            tbl.Cell(tbl.Rows.Count + 1, j).Range.Text = sum
        Next j
    End Sub
    Last edited by georgiboy; 08-22-2024 at 05:04 AM. Reason: Removed spam

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,217
    Location
    Shame about the obvious spam attack.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •