Delete rows based on cell value

swartzfeger

New Member
Joined
May 23, 2022
Messages
17
Platform
  1. Windows
  2. MacOS
Let's say I have 90 rows of data, A2:A91, and I have a cell C2 that is always changing, telling me how many rows I should have, let's say C2 = 50.

How do I write the VBA to tell it to delete any row after A51?

The number imported into A will always be changing, as will the actual number of rows needed in C2, so I'm looking for an easy way to make this idiot proof for the user without having to sort and filter.

Thank you so much for any pointers!
 
You are welcome.
Just note that in your original question, the number in that cell was the number of data rows you wanted, so you needed to add 1 to delete the first row after that.
But you also appeared to have a header row, pushing that down one more row, which is why we added 2 in the original code.
Just be aware of those things as you make changes (as the last version of code you marked as the solution would not work on your original question, as it was described).

Yup, I did this after realizing I was losing two rows:

VBA Code:
Sub DeleteRowsBelow()
    Dim r As Long
    r = Range("D1").Value
    Rows(r + 2 & ":" & Rows.Count).Delete
End Sub

Just your initial solution has allowed me to expand this greatly in other sheets using similar macros. Thanks again, you made my week!
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You are welcome.

Note if you wanted to use the other solution (if you like the shorter one), just add 2 there too, i.e.
Rich (BB code):
Sub DeleteRowsBelow()
    Rows(Range("D1").Value + 2 & ":" & Rows.Count).Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,919
Members
452,949
Latest member
beartooth91

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