VBA to match number of rows in a secondary table to the primary table automatically

LotsOfQuestions88

New Member
Joined
Sep 2, 2016
Messages
17
[FONT=&quot]Hi![/FONT]
[FONT=&quot]I've been trying for ages (not good at VBA!) to solve this with no success - I'm sure it's very simple but I'm not winning![/FONT]
[FONT=&quot]I have a primary data table (Table1) in Sheet1 and a secondary table (Table28) on a different sheet, Sheet2. Table 28 is set up to pull through some of the column data from Table 1 if entries in a certain column in Table 1 meet a criteria. [/FONT]
[FONT=&quot]The problem I have is trying to make Table 28 expand as Table 1 expands so that they have the same number of rows as the data need to match relative row positions within the tables. I have tried the following ( Sheet3 C2 contains a formula which gives the total number of rows for Table 1 and H2 gives total number of rows for Table28 ) - the idea was to add a row if the total number of rows in Table28 are less then in Table1: [/FONT]
[FONT=&quot]Sub Table_add_row()
'
' Add row to bottom of table

With ActiveSheet.ListObjects("Table28").Range.Select

If Worksheets("Sheet3").Range("C2").Value > Worksheets("Sheet3").Range("H2").Value Then

Selection.ListObject.ListRows.Add AlwaysInsert:=False

Range("A1").Select

Else: Range("A1").Select

End If

End With

End Sub[/FONT]

[FONT=&quot]However, this always end with a runtime error 9, subscript out of range error.

[/FONT]

[FONT=&quot]Ideally, this would happen automatically rather than having to insert a button to trigger a macro.[/FONT]
[FONT=&quot]Does anyone have any suggestions? Many thanks in advance for your help![/FONT]
[FONT=&quot]BW[/FONT]
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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