delete 5 cells if the value of another cell is 5

jerry1992

New Member
Joined
Sep 13, 2021
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hello, I would like to be able to delete the number of cells in a column, with the number of cells being deleted based on the
value of another cell.

For example if H2 = 5
then when I run the macro it would delete the bottom 5 cells in Column H with the range of the deletion being H3:34

So if H2=5 then it would delete H34, H33, H32, H31, and H30.

also I would like the cells to shift up when deleted so im guessing I'd use Shift:=xlUp?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I actually just realized I posted this entirely incorrect.
If H2=5 then I need the cells outside of the 5 cells at the top deleted.
So if H2= 5 then I would like to delete H9-34
This complicates things and Im sorry for that as I do not see an option to delete the post so I can create another.
 
Upvote 0
Sub delzdstaA2()
If Range("H2").Value = 0 Then
ActiveSheet.Range("H3:H34").Select
Selection.Delete Shift:=xlUp
End If
If Range("H2").Value = 1 Then
ActiveSheet.Range("H4:H34").Select
Selection.Delete Shift:=xlUp
End If
If Range("H2").Value = 2 Then
ActiveSheet.Range("H5:H34").Select
Selection.Delete Shift:=xlUp
End If
If Range("H2").Value = 3 Then
ActiveSheet.Range("H6:H34").Select
Selection.Delete Shift:=xlUp
End If
If Range("H2").Value = 4 Then
ActiveSheet.Range("H7:H34").Select
Selection.Delete Shift:=xlUp
End If




ActiveSheet.Range("F2").Select

End Sub

did that all the way to H2 = 32. Probably not the most efficient but it works
 
Upvote 0
Solution

Forum statistics

Threads
1,214,825
Messages
6,121,788
Members
449,049
Latest member
greyangel23

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