david.paton
Registered User.
- Local time
- Today, 04:59
- Joined
- Jun 26, 2013
- Messages
- 338
I have a table added to my spreadsheet and I have two buttons. One to add a row to the table and one to delete a row from the table. When I put them in, they both will work but then I go and do some other formatting on the spreadsheet and I come back to the buttons and I click them and only the add button works, the delete button doesn't do anything.
It is really strange and I have no idea what to do as I don't know how to code, the code behind the buttons I found on the net.
This is the code I have:
Private Sub CommandButton3_Click()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("table3")
'add a row at the end of the table
tbl.ListRows.Add
End Sub
Private Sub CommandButton4_Click()
Dim oLst As ListObject
Application.ScreenUpdating = False
If ActiveSheet.ListObjects.Count > 1 Then
For Each oLst In ActiveSheet.ListObjects
With oLst
If .Name = "Table3" Then
If oLst.ListRows.Count > 1 Then
number_of_columns = .ListColumns.Count
oLst.ListRows(oLst.ListRows.Count).Delete
End If
End If
End With
Next
End If
End Sub
Can anyone see what is wrong with this code or maybe a better way to code it?
Thanks,
Dave
It is really strange and I have no idea what to do as I don't know how to code, the code behind the buttons I found on the net.
This is the code I have:
Private Sub CommandButton3_Click()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("table3")
'add a row at the end of the table
tbl.ListRows.Add
End Sub
Private Sub CommandButton4_Click()
Dim oLst As ListObject
Application.ScreenUpdating = False
If ActiveSheet.ListObjects.Count > 1 Then
For Each oLst In ActiveSheet.ListObjects
With oLst
If .Name = "Table3" Then
If oLst.ListRows.Count > 1 Then
number_of_columns = .ListColumns.Count
oLst.ListRows(oLst.ListRows.Count).Delete
End If
End If
End With
Next
End If
End Sub
Can anyone see what is wrong with this code or maybe a better way to code it?
Thanks,
Dave
Last edited: