Using VBA to add/delete blank rows below a selected cell/row to a certain row number?

ubergreen

New Member
Joined
Jun 13, 2024
Messages
33
Office Version
  1. 2021
I want to be able to add a button that can add blank rows to a selected cell on my sheet. For example

Order NumberColorSize
1BlueL
GreenS
2PinkM
3GreyXL



Then I want to be able to select say cell either A3/B3/etc and press a button that adds two blank rows below to result in the below

Order NumberColorSize
1BlueL
GreenS
2PinkM
3GreyXL


Then after that is complete, i want to be able to push another button, while selecting cell A5/B5/etc and then press a button that deletes all of the rows above it to move it to the top underneath the header as below:
Order NumberColorSize
2PinkM
3GreyXL
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Welcome to the Board!

Here is code for your row insert:
VBA Code:
Sub InsertTwoBlankRowsBelow()
    Dim r As Long
    r = ActiveCell.Row
    Range(Cells(r + 1, "A"), Cells(r + 2, "A")).EntireRow.Insert
End Sub

And here is code for the row delete:
VBA Code:
Sub DeleteRowsAbove()
    Dim r As Long
    r = ActiveCell.Row
    If r > 2 Then Range(Cells(2, "A"), Cells(r - 1, "A")).EntireRow.Delete
End Sub

Just attach that code to your buttons, and you should be good to go!
 
Upvote 0
Solution
Welcome to the Board!

Here is code for your row insert:
VBA Code:
Sub InsertTwoBlankRowsBelow()
    Dim r As Long
    r = ActiveCell.Row
    Range(Cells(r + 1, "A"), Cells(r + 2, "A")).EntireRow.Insert
End Sub

And here is code for the row delete:
VBA Code:
Sub DeleteRowsAbove()
    Dim r As Long
    r = ActiveCell.Row
    If r > 2 Then Range(Cells(2, "A"), Cells(r - 1, "A")).EntireRow.Delete
End Sub

Just attach that code to your buttons, and you should be good to go!

Thanks for the codes. The codes worked well.

I actually just realized that I wrote my question incorrectly. What I need it to do is add blank rows up to a specific row number. Using the example above, I would like to be able to select maybe the last item in the row and then be able to click a button so that it will add blank rows up to row # 5 every single time. Regardless of its initial starting row position.

However, thinking about possibly being able to automate it more, I don't know if this is something that is possible with either VBA or PQ. Would it somehow be possible to set it up in PQ so that each of the "Order Number" groups will have blank rows added to the end of each group so that each group always takes up exactly 5 rows. Example below

Screenshot 2024-06-14 083023.jpg


Then with PQ, have it automatically output to look like the following below:

Screenshot 2024-06-14 083240.jpg


Then from here, if I can use VBA to create a button that will always delete only the top 5 rows, so that it will essentially move the next order group to the top 5 rows and so on. The top row with order number, color and size will be table headers, so I don't think they will be affected by the VBA code from what I understand.
 
Upvote 0
Your follow-up question is VERY different than your original question (and as you said, the initial supply answered the actual question you posted).
There, you should post it in a new thread/question of its own (especially since you are now introducing Power Query, which I do not work with).

Also note that there is a forum here where you can ask Power Query questions: Power Tools
 
Upvote 0
Your follow-up question is VERY different than your original question (and as you said, the initial supply answered the actual question you posted).
There, you should post it in a new thread/question of its own (especially since you are now introducing Power Query, which I do not work with).

Also note that there is a forum here where you can ask Power Query questions: Power Tools

You're right. Thank you for the help!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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