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!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
So something like this:

Book1.xlsm
ABCD
1datatotal rows5
2ABC
3DEF
4GHI
5JKL
6MNO
7PQR
8STU
9VWX
10YZ
11123
12456
13789
Sheet1


The script would then automatically delete rows 7-13 based on D2:

Book1.xlsm
ABCD
1datatotal rows5
2ABC
3DEF
4GHI
5JKL
6MNO
Sheet1


Thanks for any pointers!
 
Upvote 0
So I need to do something like

Range("a7:a13").EntireRow.Delete

But I need to figure out how to keep any rows less than <D1 and delete anything greather than >D1

I could do something like this:

Sub DeleteAllBelow()
Rows(ActiveCell.Row & ":" & Rows.Count).Delete
End Sub

But that would require the user to pay attention, not misclick on the wrong row, etc.
 
Upvote 0
In your original question, you say the number is in C2.
In your follow-up, you see it is in cell D2.
But in your image, you show it in cell D1.
So where exactly is this number?
 
Upvote 0
If it is cell D1, then something like this would work:
VBA Code:
Sub MyDeleteRows()

    Dim lr As Long
    Dim r As Long
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Get value from cell D1
    r = Range("D1").Value
    
'   Delete rows
    If lr - r > 1 Then Rows(r + 2 & ":" & lr).Delete
        
End Sub
 
Upvote 0
If it is cell D1, then something like this would work:
VBA Code:
Sub MyDeleteRows()

    Dim lr As Long
    Dim r As Long
   
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Get value from cell D1
    r = Range("D1").Value
   
'   Delete rows
    If lr - r > 1 Then Rows(r + 2 & ":" & lr).Delete
       
End Sub

Joe I deeply apologize I tried to go back and edit the post/s to clear up confusion but I was unable. Yes, it's D1.

It worked perfectly, and I can see the logic of what you did -- grab the total range and then lr - r. The problem I had figuring out was how to declare/dim D1 so I could then use it to calculate. Thank you SO MUCH!!!

Could something like this also work?

Sub DeleteRowsBelow()
Rows("D1" & ":" & Rows.Count).Delete
End Sub

Or would I need to declare D1 as a value to be used to determine the row number?

Thanks again, learning this stuff is fun :D
 
Upvote 0
Sub DeleteRowsBelow()
Rows("D1" & ":" & Rows.Count).Delete
End Sub

Or would I need to declare D1 as a value to be used to determine the row number?
If cell D1 contains the first row number of the row you would like to delete, then it would need to look like:

VBA Code:
Sub DeleteRowsBelow()
    Rows(Range("D1").Value & ":" & Rows.Count).Delete
End Sub
 
Upvote 0
Solution
So I just did this and it works too!!!

VBA Code:
Sub DeleteRowsBelow()
    dim r as long
    r = Range("D1").Value
    Rows(r & ":" & Rows.Count).Delete
End Sub

Thank you again, Joe!!!
 
Upvote 0
If cell D1 contains the first row number of the row you would like to delete, then it would need to look like:

VBA Code:
Sub DeleteRowsBelow()
    Rows(Range("D1").Value & ":" & Rows.Count).Delete
End Sub

Ok I just missed your post as I was posting, I like your way better because it's cleaner. Thank you again for all your help.
 
Upvote 0
So I just did this and it works too!!!

VBA Code:
Sub DeleteRowsBelow()
    dim r as long
    r = Range("D1").Value
    Rows(r & ":" & Rows.Count).Delete
End Sub

Thank you again, Joe!!!
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).
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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