Re: Insert new row in table when one empty row remains via vba
Still looking for an answer to the question:
Is there a way to insert the row IN the table, but BELOW the selected cell; especially when the selected cell is the last one in the table?
Interesting thought that I am not sure how to implement:
instead of inserting a row below the last row on the table, could the code insert a row below the table itself, then expand the table down one row? That would increase the variable "mp" by 1, effectively breaking the loop, but allow the user to manipulate the last cell without having blank rows inserted above the final row in the table... Someone have a bit of code in their head to do that?
I'll just keep editing this post to make things easy...
Code thus far:
'Set rd = Sheets("ROLL UP")
mp = ListObjects("Table14").ListRows.Count + 1
While Cells(mp, "B") <> ""
Sheet1.Cells(mp + 1, "B").EntireRow.Insert
rd.ListObjects("Table14").Resize Range("$A$1:$AD$" & mp + 1)
mp = ListObjects("Table14").ListRows.Count
Wend
Now, this code runs to the EntireRow.Insert, inserts the row below the table, then resets to the "Set rd = " line. I am guessing that it has something to do with the mp range variable changing, but I had to force the code to reset mp at the end because it was initially holding the mp variable even after inserting a row in the table. Anyone want to guess why this thing goes into an infinite loop and refuses to go past inserting a row without resetting the entire macro? The best part of this thing is that it's in a Worksheet_Change(ByVal Target As Range) sub, so I cannot F8 into it, I have to put breaks and F5 through the thing on every iteration. If anyone wants to check this thing on a test workbook, the attachment is on the first post, replace the code on sheet1 with
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrHandler
Set rd = Sheets("ROLL UP")
mp = ListObjects("Table14").ListRows.Count + 1
While Cells(mp, "B") <> ""
Sheet1.Cells(mp + 1, "B").EntireRow.Insert
rd.ListObjects("Table14").Resize Range("$A$1:$AD$" & mp + 1)
mp = ListObjects("Table14").ListRows.Count
MsgBox ("I have inserted a row")
Wend
'// Any column but R changed? Replace ?? with GENDER Col Number
If Target.Column <> 18 And Target.Column <> 5 Then
Exit Sub
End If
'// If Target row is within range of table14 - Just added a check to ignore header
If Target.Row <= ListObjects("Table14").ListRows.Count + 1 And Target.Row > 1 Then '// + 1 to include the header...
Application.EnableEvents = False
If UCase$(Target.Value) <> "G" And Target.Column = 18 Then
Target.Formula = "=IF(Table14[[#This Row],[SEX]]=" & Chr$(34) & _
"M" & Chr$(34) & "," & Chr$(34) & "NA" & Chr$(34) & _
"," & Chr$(34) & "NG" & Chr$(34) & ")"
Application.EnableEvents = True
ElseIf Target.Column = 5 Then '// Gender column number in here
Range("R" & Target.Row).Formula = "=IF(Table14[[#This Row],[SEX]]=" & Chr$(34) & _
"M" & Chr$(34) & "," & Chr$(34) & "NA" & Chr$(34) & _
"," & Chr$(34) & "NG" & Chr$(34) & ")"
Application.EnableEvents = True
End If
End If
ErrHandler:
Application.EnableEvents = True
End Sub
Display More
best of luck.