I have a button that users can click which adds a row to a given table on a given sheet. If the user has selected a cell within the table, then the macro determine it needs to add a row directly below the selection. If the user is not in the table, then it simply adds a row to the bottom of the table.
It works rather well except I can't figure out how to copy formatting from the row above. Is this possible?
My code which may be of use is below:
Private tblTotalRows As Integer
Public selectedRow As Integer
Public selectedCol As Integer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Add a row to the table
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub addRow(shtName As String, tblName As String, startRow As Integer)
Dim tableRef As Integer
Call getSelectedCell
Call totalRowsInTable(shtName, tblName, startRow) ' Sets the selectedRow and tblTotalRows property
' We determine the row number where the new table row should be placed
tableRef = selectedRow - startRow + 1
' Check to make sure the user is in the active table and then add a row
If tableRef > 0 And selectedRow <= tblTotalRows Then
Sheets(shtName).ListObjects(tblName).ListRows.Add (tableRef)
Else
' If they're not in the table and then add a row to the bottom of the table
Sheets(shtName).ListObjects(tblName).ListRows.Add AlwaysInsert:=True
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get the row and column of the cell under selection
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub getSelectedCell()
selectedRow = ActiveCell.Row
selectedCol = ActiveCell.Column
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Count the number of rows in the Table
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub totalRowsInTable(shtName As String, tblName As String, startRow As Integer)
Call getSelectedCell
' Select the entire table
Sheets(shtName).ListObjects(tblName).Range.Select
' Count the number of rows in the table and add to the starting row
tblTotalRows = Selection.Rows.Count + startRow - 1
' Go back to the users position
Cells(selectedRow, selectedCol).Select
End Sub