Adding new row macro works but is affected when new rows are inserted above it

Cauz

New Member
Joined
Jul 16, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello world,

My supervisor asked me to generate a meeting minute with macro features on Excel. So am basically baby new to VBA.

I was able to create a button that adds rows which works.

Private Sub CommandButton1_Click()

Sheets("Minutes_temp"). Range ("A32"). Select
ActiveCell.EntireRow.Insert Shift:=xlDown

Sheets("Minutes_temp"). Range("A32:D32"). Select
Selection.Borders.Weight = xlThin

End Sub

Now my challenge is .... When I insert new rows above row A32 in the worksheet it changes the flow of the macro button when clicked on to "add new row".
I will like my "add new row" button to add rows below when clicked on and not being affected if rows are inserted above it.

Thanks for your solutions in advance.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Welcome to the forum
In Excel, a named cell re-references automatically if you insert a row above it
And we can use that in VBA

Select the cell above where you want row inserted
Put InsertHere In the name box (found above cell A1) and hit {ENTER}

VBA Code:
Private Sub CommandButton1_Click()
    Range("InsertHere").Offset(1).EntireRow.Insert Shift:=xlDown
    Range("InsertHere").Offset(1).Resize(, 4).Borders.Weight = xlThin
End Sub

Name Box 2.jpg



Note - to remove or edit the Named Range, click on Formulas \ Name Manager
 
Upvote 0
Thanks again yongle.
So i tried using the code you suggested to add rows as i enter data into my worksheet. I noticed it solves the problem of not changing the flow of my data set when i insert new rows above cell "A32" where my table is....

But, instead of increasing incrementally referencing the last added row... It keeps adding rows beneath my named cell A32= "InsertHere" presently.
So when i begin to enter data into my worksheet, lets say am typing in cell A40 and click on my my macro button to "add row" instead of adding row "A41" it takes be back directly below "A32" to add row.

I will like to know how i can get it to work properly?

Thanks for responses in advance.
 
Upvote 0
I do not understand what you want
Perhaps ???
VBA Code:
Private Sub CommandButton1_Click()
    Range("A" & Rows.Count).End(xlUp)(2).Resize(, 4).Borders.Weight = xlThin
End Sub
 
Upvote 0
MrexcelQuestionOnAddRow.jpg



The image describes the problem. Thanks for your responses.

I entered a in C30, b in C31 and originally c in C32
I then clicked on my "Add Agenda" button to insert new row
The action moves my entry c in C32 downwards to C33 and inserted a new row above c

What i want is that a new row inserted will always be the last row. i,e as below c
And also, if rows are added above the AGENDA session of my worksheet. It won't affect the code/flow of your anticipated solution.

Below is my current code

Private Sub CommandButton1_Click()

Range("InsertHere").Offset(1).EntireRow.Insert Shift:=xlDown
Range("InsertHere").Offset(1).Resize(, 4).Borders.Weight = xlThin

End Sub

Thanks again.

N: i used named cell (InsertHere) which you suggested because my previous code action was affected whenever a row is inserted above my AGENDA session in my worksheet
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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