Word – Count Table Cells in Selection
Say you have a Word table with lots of empty rows and simply want to know how many rows there are, select them and run the following VBA code:
If Selection.Information(wdWithInTable) Then
MsgBox “Cells in selection: ” & Selection.Cells.Count
This counts cells in selection, so if you select a range of cells over multiple columns and rows, it will count all of them.
If you merge cells, the merged range will count as 1 cell.
When is this useful?
If you want to paste data from Excel for example, and you have 18 cells in the Excel copied range, if in the Word table you click only one cell in the desired column and Paste, the values will most probably be inserted in the one cell. If you select less than 18 cells in Word for pasting, then not all your data will be pasted, and if you select more than 18 cells, then you’ll have duplicate pasting in some cells.
That’s why you want to count the cells in a selection and know it is the correct selection, then paste.