Using macro button how to insert rows in two non formatted tables

wakefield101

New Member
Joined
Jan 4, 2014
Messages
31
I have two unformatted tables in my excel worksheet. The tables are vertical from one another. I have successfully created a macro button that adds 1 row under the header of the first table. However, adding a row to the second table simultaneously is where I am having trouble.

What I want to accomplish: Create a button that will add row below the header of unformatted table 1 THEN add a row below the header of the second table.
Goal: If I need to add 10 lines to each table, all I need to do is click my add row button ten time.

The issue that I am having is that the row reference for table 2 is not adjusting when a new row is added to table 1. When I insert a row in table 1 it shifts all data down one row. Then Table 2 is referencing a row above where it should be rather than adjusting to the change.

Does anyone know how to resolve this issue?


Here is my current VBA Code.

Sub AddRow_24MoGrid()
'
' Add Row_GridFcst Macro
'

Sheets("SF Client Grid 24mo Fcst").Rows("13:13").Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

Sheets("SF Client Grid 24mo Fcst").Rows("149:149").Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

End Sub
 

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).
If indeed you have two Tables on your worksheet, you might give the following a try...

Code:
Sub InsertTableRow()
Dim i As Long
For i = 1 To 2
    Sheets("SF Client Grid 24mo Fcst").ListObjects(i).ListRows.Add (1)
Next i
End Sub

Cheers,

tonyyy
 
Upvote 0
Thanks Tony!

However they are not technically "tables"... That is why I was referring to them as "unformulated tables"...... Does this make sense?
 
Upvote 0
So, you didn't use the Insert / Table command, right? (And why not?? [just kidding])

Sounds like you have two ranges, each with a header row. The header row of the first range is in Row 12 - please confirm.

As for the second header row, we'll have to find it. What's the value in the first and second column of the second header row?
 
Upvote 0
Hi Tony, sorry for the delayed response.

There are too many complications, that is why I am not using tables.

Yes, I do have two ranges, each with a header.

I just created an example model with the code that I am using in my actual model, however I just realized that I cannot add an attachment....... hum do you have any thoughts on how I can share my example with you and/or this thread in general??

Let me know what you think.
 
Upvote 0
I just created an example model with the code that I am using in my actual model, however I just realized that I cannot add an attachment....... hum do you have any thoughts on how I can share my example with you and/or this thread in general??

Refer to the Guidelines for Forum Use...
 
Upvote 0
Okay, I figured it out.

Here is the Macro code that I am currently using. I set it up as a button.


Sub AddRow_24MoGrid()
'
' Add Row_GridFcst Macro
'

Sheets("Sheet1").Rows("6:6").Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

Sheets("Sheet1").Rows("16:16").Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

End Sub




Excel Workbook
BCDEFGHIJKL
33CDEFGHIJKL
44**********
55*Unit No.Unit NameUnit BasisUnit OwnerContract Value *Sold Unit PriceTotal Sold Units*Add Row Button
66**********
77**********
88**********
99**********
1010**********
1111**********
1212**********
1313**********
1414*Unit No.Unit NameUnit BasisUnit OwnerContract Value *Sold Unit PriceTotal Sold Units**
1515**********
1616**********
1717**********
1818**********
1919**********
2020**********
Sheet1
 
Upvote 0
wakefield101,

I made a few changes to your sheet...
*Deleted all merged cells (vba handles merged cells very poorly)
*Placed "Unit" and "No." into a single cell, and
*Increased the row height of the header rows to 30.

If those changes are okay with you then you might give the following a try...

Code:
Sub AddRow_24MoGrid()
Dim FoundHeader As Range

Sheets("Sheet1").Rows("6:6").Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

Set FoundHeader = Sheets("Sheet1").Columns(4).Find("Unit No.", After:=Range("D6"), _
    LookIn:=xlValues, LookAt:=xlWhole, searchdirection:=xlNext)
FoundHeader.Offset(1, 0).EntireRow.Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub
 
Upvote 0
Thanks Tonyy,

To test your code out, I applied it to my example model. However, I received an error "Run-time error '91' Object variable or With block variable not set"

Once I clicked Debug, VBA highlighted the entire row: "FoundHeader.Offset(1,0).EntireRow.Select"

Any idea on how to fix this?
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,015
Members
449,060
Latest member
LinusJE

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