-
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 :-)
-
Maybe something like this:
Code:
Sub AddCol()
Dim t As Table
Set t = ActiveDocument.Tables(1)
t.Columns.Add
t.Columns.Autofit
End Sub
-
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.).
-
Here's a VBA macro that will add up the columns in a Word table, even if there are empty cells:
Code:
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
-
Shame about the obvious spam attack.