View Full Version : [SOLVED:] Adding additional rows to table
Aussiebear
05-13-2024, 06:16 PM
The following code adds an additional row after each odd row
Sub Tbl_Add_Rows()
    'In the selected tbl, insert a row after each odd row.
    Dim LastRow As Integer
    Application.ScreenUpdating = False
    LastRow = selection.Tables(1).rows.count
    If LastRow Mod 2 = 0 Then
        LastRow = LastRow
    Else: LastRow = LastRow - 1
    End If
    For i = LastRow To 2 Step -2
        selection.Tables(1).rows(i).Select
        With selection.Tables(1).rows(i)
            selection.InsertRowsAbove 1
        End With
    Next i
    Application.ScreenUpdating = True 
End Sub
I'm assuming then that if one knows the "pattern" of when you would like a row inserted you need to adapt either Mod(?) or For i = LastRow to 2 step(?) or both to adjust the insertion point.  Is this correct?
Aussiebear
05-14-2024, 09:41 PM
Anyone care to comment?
georgiboy
05-14-2024, 10:48 PM
The MOD part looks if the last row of the table is odd, if it is it subtracts 1 from the last row variable to make it even. So I suppose if you want it to start from an odd row you could omit that part.
With Step set to -2.
MOD part removed:
1
2
 
3
4
 
5
6
 
7
MOD part kept:
1
 
2
3
 
4
5
 
6
7
As for the loop part, you could adjust the step to make it insert rows every 3 or 4 etc. If you wanted to insert a row every other row (Step -1) then you would need to omit the MOD part unless your table ended on an even row, otherwise it will not add a row until the first even row.
For example with Step set to -1 and a table with 7 rows.
With MOD:
1
 
2
 
3
 
4
 
5
 
6
7
Without MOD:
1
 
2
 
3
 
4
 
5
 
6
 
7
Aussiebear
05-15-2024, 12:22 AM
Interesting.   As i was trying to understand the initial concept was to insert a new row every third row, and that there must have been a header row.
gmaxey
05-15-2024, 12:41 AM
Ted,
   I think I would use a Do ... Loop Until with an error handler.  Also I wouldn't use the selection object:
Sub Tbl_Add_RowsII()
Dim oTbl As Table
Dim oRow As Row
Dim lngSeq As Long
  Application.ScreenUpdating = False
  Set oTbl = Selection.Tables(1)
  lngSeq = 2 'Use 2 to insert row after each odd or even row. Use 3 for every third, 4 for every fourth, etc.)
  Set oRow = oTbl.Rows(lngSeq) 'For every odd row
  'Set oRow = oTbl.Rows(lngSeq + 1) 'For every even row, or other sequence (e.g., every third, fourth, fifth etc.)
  Do
    oTbl.Rows.Add oRow
    On Error GoTo Err_Index
    Set oRow = oTbl.Rows(oRow.Index + lngSeq)
  Loop Until oRow.Index = oTbl.Rows.Count
  oTbl.Rows.Add oRow
lbl_Exit:
  Exit Sub
Err_Index:
  If oTbl.Rows.Last.Index - oRow.Index = lngSeq - 1 Then
    oTbl.Rows.Add
  End If
  Resume lbl_Exit
End Sub
Aussiebear
05-15-2024, 05:27 AM
Now thats a different approach to the subject, and conversely for inserting extra columns it could be
Sub Tbl_Add_ColumnsII()
    Dim oTbl As Table
    Dim oCol As Column
    Dim lngSeq As Long
    Application.ScreenUpdating = False
    Set oTbl = Selection.Tables(1)
    lngSeq = 2 'Use 2 to insert row after each odd or even Column. Use 3 for every third, 4 for every fourth, etc.)
    Set oCol = oTbl.Columns(lngSeq) 'For every odd Column
    'Set oCol = oTbl.Columns(lngSeq + 1) 'For every even Column, or other sequence (e.g., every third, fourth, fifth etc.)
    Do
         oTbl.Columns.Add oCol
         On Error GoTo Err_Index
         Set oCol = oTbl.Columns(oCol.Index + lngSeq)
    Loop Until oCol.Index = oTbl.Columns.Count
    oTbl.Columns.Add oCol
    lbl_Exit:
    Exit Sub
    Err_Index:
    If oTbl.Columns.Last.Index - oCol.Index = lngSeq - 1 Then
         oTbl.Columns.Add
    End If
    Resume lbl_Exit 
End Sub
gmaxey
05-15-2024, 06:00 AM
Ted,
   Yes.  Then only thing I would do differently is:
Sub Tbl_Add_ColumnsII()
Dim oTbl As Table
Dim oCol As Column
Dim lngSeq As Long
  Application.ScreenUpdating = False
  Set oTbl = Selection.Tables(1)
  
  lngSeq = 2 'Use 2 to insert row after each odd or even Column. Use 3 for every third, 4 for every fourth, etc.)
  'Set oCol = oTbl.Columns(lngSeq) 'For every odd Column
  Set oCol = oTbl.Columns(lngSeq + 1) 'For every even Column, or other sequence (e.g., every third, fourth, fifth etc.)
  Do
    oTbl.Columns.Add oCol
    On Error GoTo Err_Index
    Set oCol = oTbl.Columns(oCol.Index + lngSeq)
  Loop Until oCol.Index = oTbl.Columns.Count
  oTbl.Columns.Add oCol
lbl_Exit:
  oTbl.AutoFitBehavior wdAutoFitWindow  'Added GKM
  Exit Sub
Err_Index:
  If oTbl.Columns.Last.Index - oCol.Index = lngSeq - 1 Then
    oTbl.Columns.Add
  End If
  Resume lbl_Exit
End Sub
Vladimir
05-15-2024, 10:30 AM
Hi! Since I am the one (vivka) who suggested the original code at a different forum, I can offer an improved version of it:
Sub Tbl_Add_Row()
'In the selected tbl, insert a row after each n-th row.
Dim LastRow As Integer, RemainderRows As Integer, CountBy As Integer
    Application.ScreenUpdating = False
    
    CountBy = InputBox("Enter the number to count rows by:", "ENTER A ROW AFTER EACH...")
    LastRow = selection.Tables(1).rows.count
    RemainderRows = LastRow \ CountBy
    
    If LastRow Mod CountBy = 0 Then
        LastRow = LastRow
    Else: LastRow = CountBy * RemainderRows
    End If
    For i = LastRow To 2 Step -CountBy
        selection.Tables(1).rows(i).Select
        With selection.Tables(1).rows(i)
            selection.InsertRowsBelow 1
        End With
    Next i
    Application.ScreenUpdating = True
End Sub
gmaxey
05-15-2024, 10:38 AM
Vivka,
  This code doesn't address the question posted here.  Code to insert a row after each odd row.  Also, why do you declared some variables e.g., LastRow, Countby etc., but not others e.g., i?
Vladimir
05-15-2024, 11:49 AM
gmaxey, thank you for your remarks! I'm still learning vba and, naturally, making mistakes. But, hopefully, I'm slowly progressing with the tips and codes from experts at different forums (you and Aussiebear are among those experts).
Aussiebear
05-15-2024, 02:26 PM
... (you and Aussiebear are among those experts)..  Tis only one there who's the "expert' Vladimir, and it definitely isn't me.
Vladimir
05-16-2024, 06:53 AM
:rotlaugh:
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.