Log in

View Full Version : Add columns in Word



KiwiAlex
08-18-2024, 03:56 PM
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 :-)

Aussiebear
08-19-2024, 12:35 AM
Maybe something like this:



Sub AddCol()
Dim t As Table
Set t = ActiveDocument.Tables(1)
t.Columns.Add
t.Columns.Autofit
End Sub

Vladimir
08-19-2024, 02:37 AM
Hi, KiwiAlex (http://www.vbaexpress.com/forum/member.php?88379-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.).

elenagilbert
08-21-2024, 07:03 PM
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

Aussiebear
08-22-2024, 02:28 AM
Shame about the obvious spam attack.