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?
 
Here is what my chart looked like:

YearSavings
120
222
324
426
528
630
732
834
936
1038
1120
1222
1324
1426
1528

<tbody>
</tbody>


And here is the whole code:
Code:
Sub CreateTable() 'Creating a tableActiveSheet.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




'Sees if Life Expectancy is higher than the amount of rows, and adds rows accordingly
If Life > rows Then
Do Until Life = rows
ListObjects("Table1").ListRows.Add
AlwaysInsert = True
rows = Range("Table1").rows.Count
Loop
End If




'Sees if Life Expectancy is lower than the amount of rows, and deletes rows accordingly
If Life < rows Then
Do Until Life = rows
ActiveSheet.ListObjects("Table1").ListRows(rows).Delete
rows = Range("Table1").rows.Count
Loop
End If
End Sub


Sub FillTable()
    Dim MyTable As ListObject
    Dim Used As Range
    With ActiveSheet
        Set MyTable = .ListObjects("Table1")
        Set Used = .Range("A8:A" & .Range("A7").End(xlDown).Row).Resize(, 2)
        If MyTable.ListRows.Count > Used.rows.Count Then
            Used.AutoFill MyTable.DataBodyRange
        End If
    End With
End Sub

Did I miss type something?
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Oh, I think I forgot to mention that the Savings Column has a formula in it rather than just counting. So the first row is just the number 20, but the second is =B8+2 and so on for the rest.
 
Upvote 0
I should have tested it, sorry:

Rich (BB code):
Sub FillTable()
    Dim MyTable As ListObject
    Dim Used As Range
    With ActiveSheet
        Set MyTable = .ListObjects("table1")
        Set Used = .Range("A9:A" & .Range("A7").End(xlDown).Row).Resize(, 2)
        If MyTable.ListRows.Count > Used.Rows.Count Then
            Used.AutoFill MyTable.DataBodyRange.Offset(1).Resize(MyTable.DataBodyRange.Rows.Count - 1)
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,310
Members
449,152
Latest member
PressEscape

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