Adding or Subtracting Rows in a Table Based on a Cell Value

ashperson95

New Member
Joined
May 16, 2014
Messages
36
Okay, I am trying to create a program that creates a table, and then if the value of a certain cell is larger or smaller than the amount of rows already in the table (the table has a standard 10 rows with data), then it adds or subtracts based on that value. I'm not very familiar with VBA language, but I do have some background in Java programming (although it's been several years). I've only gotten to the adding rows part before I came up with an error message and I can't figure out what I did wrong.

Here is what I have so far:
Sub CreateTable()
'Creating a table
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$7:$B$17"), , xlYes).Name = "Table1"
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
End Sub


Sub EditRows()
Dim rows As Long
Dim Life As Long
'Gets value of Life Expectancy
Life = Worksheets("Sheet1").Cells(2, "D").Value
'Counts rows in table
rows = Range("Table1").rows.Count


If Life > rows Then Data.ListObjects("Table1").ListRows.Add
'This line gives me a 424 Object Required message
End Sub

Like I said, I am unfamiliar with VBA language, so is my syntax wrong or am I just completely wrong?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
That worked, thank you! I know that probably seemed like a stupid question.
I'll most likely be posting later as I move on into doing this function in a loop until all needed rows have been added, and then filling in the data in the new rows
 
Upvote 0
Dreadknight, I will be trying to loop it, I wanted to get the adding rows function first. I'm fairly new to VBA language so I'm doing everything in tiny steps
 
Upvote 0
Ugh, okay, so I've run into another problem.
I thought deleting a row would be similar to adding one, but I've gotten the syntax wrong again. I tried googling it and the way I currently have it is the only way I can find, but I'm still getting error messages.

Sub CreateTable()
'Creating a table
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$7:$B$17"), , xlYes).Name = "Table1"
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
End Sub


Sub EditRows()
Dim rows As Long
Dim Life As Long
'Gets value of Life Expectancy
Life = Worksheets("Sheet1").Cells(2, "D").Value
'Counts rows in table
rows = Range("Table1").rows.Count


Do Until Life = rows
If Life > rows Then ListObjects("Table1").ListRows.Add
AlwaysInsert = True
If Life < rows Then ActiveSheet.ListObjects("Table1").ListRows.Delete
'Gives me runtime error 438
rows = Range("Table1").rows.Count
Loop
End Sub

Help please!
 
Upvote 0
The ListRows object doesn't have a Delete method. You need to provide it with an Index to return a ListRow object, eg:

Rich (BB code):
ActiveSheet.ListObjects("Table1").ListRows(1).Delete
 
Upvote 0
It doesn't give me the error message anymore, but it's not deleting the rows.

I've changed it a little so that the adding and deleting functions were in different loops. Here:
Code:
 Sub EditRows()Dim rows As Long
Dim Life As Long
'Gets value of Life Expectancy
Life = Worksheets("Sheet1").Cells(2, "D").Value
'Counts rows in table
rows = Range("Table1").rows.Count


If Life > rows Then
Do Until Life = rows
ListObjects("Table1").ListRows.Add
AlwaysInsert = True
rows = Range("Table1").rows.Count
Loop
End If


If Life < rows Then
Do Until Life = rows
ActiveSheet.ListObjects("Table1").ListRows(1).Delete
rows = Range("Table1").rows.Count
Loop
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top