Delete Rows. Greater than or Equal to

mayuko

New Member
Joined
Jul 8, 2011
Messages
9
Hi,

I need some help with Macro

I want to delete all rows that has values in column D with a value
greater than or equal to C.

Any help would be greatly appreciated! Thank you.
 
Sub test()
Range("D2").Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value >= ActiveCell.Offset(0, -1).Value Then
ActiveCell.Select
Selection.EntireRow.Delete
End If
If ActiveCell.Value <= ActiveCell.Offset(0, -1).Value Then
ActiveCell.Offset(1, 0).Select
End If
Loop
End Sub
 
Last edited:
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
mirabeau
Thanks, mirabeau. I am pretty sure that C is real numbers... That's what you meant, right?? I ran it then it deleted everything. I am very new to VBA, so I am so sorry I don't know what happened (;_;)
mayuko,

that code had already used (lowercase) c for another purpose, but when including your (uppercase) C, I overlooked the fact that the code would treat c and C as the same. So yes, as it stands it probably would delete everything.

As indicated, it's very easily modified, but I can't do more without knowing
how your "C" is defined. Like
(a) is your "C" a specific number ( like C=5) that you input from somewhere such as an inputbox?, or maybe as a part of the code itself?
or
(b) is your "C" a number or set of numbers located somewhere in the worksheet? where?
or perhaps some other definition of "C".
 
Upvote 0
BrandonWLH
Thank you, Brandon. When I changed and ran, it gave me "duplicate" error. I think I am not understanding it right because I am not an English speaker, so if you could post the whole code, I would really appreciate it. Thank you again.


Code:
Sub test()
     Dim rng As Range
     Set rng = Worksheets("Sheet1").Range("C1")
     Dim x As Integer
     x = 0
     Do While Not IsEmpty(rng.Offset(x, 0))
         If rng.Offset(x, 0) > rng.Offset(x, 1) Then
             rng.Offset(x, 0).EntireRow.Delete
             Set rng = Worksheets("Sheet1").Range("C1")  'this
         Else
             x = x + 1
         End If
     Loop
End Sub
 
Upvote 0
Thank you very much guys. Brandon's code worked perfectly!

I feel very lucky you guys helped with this even though I haven't met you :)

Thank you thank you thank you!
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

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