PDA

View Full Version : Splitting a particluar column into multiple rows



MCS
06-09-2023, 03:59 PM
Hi.
I'm brand new to VBA, but have coded in VB before.
In Microsoft Word, starting with a table of 33 rows and 3 columns, I want to split each of the initial 33 rows (of column 3 only) into 10 rows each.
I thought this would be pretty easy, but the code (below) doesn't work. It actually works for the first 2 (of the original) rows, but then the count somehow gets messed up and the third original row doesn't work. I'm not sure why (the count will continue to increase as I am adding 10 rows at a time). As well, the routine ends with no error messages and the job is not complete (except for the first two original rows).

The code I'm using:


Sub SplitColumnIntoRows()
' In Microsoft Word, split each of the initial 33 rows (of column 3 only) into 10 rows each.
Dim tbl As Table
Dim row As Integer
Dim rowCount As Integer
Dim col As Integer
' Set the table reference to my specific table
Set tbl = ActiveDocument.Tables(1)
' Loop through the appropriate cell in the table (column 3 only)
col = 3
rowCount = tbl.Columns(col).Cells.Count
For row = 1 To rowCount Step 10
tbl.cell(row, col).Select
' Split cell into desired number of rows
Selection.Cells.Split NumRows:=10, NumColumns:=1
rowCount = tbl.Columns(col).Cells.Count
Next
End Sub

Any illumination would be helpful.
Thank you in advance.

Aussiebear
06-10-2023, 12:15 AM
Welcome VBAX MCS. Please be patient as hopefully a word guru will be along shortly.

gmaxey
06-10-2023, 05:45 AM
So at the end of the show, you are going to have a table with 330 rows:

Sub SplitColumnIntoRows()
Dim oTbl As Table
Dim lngRow As Long
Set oTbl = ActiveDocument.Tables(1)
For lngRow = 1 To 330 Step 10
oTbl.Cell(lngRow, 3).Select
Selection.Cells(1).Split NumRows:=10, NumColumns:=1
Next lngRow
lbl_Exit:
Exit Sub
End Sub