Need Some Help with If / Then Statements

anees420

New Member
Joined
Mar 8, 2010
Messages
12
here is what i'm trying to do

I need a macro that does the following


----I have a diagram explaining what i'm trying to do ... this was the best i knew how to get my point across

click on the link to view it

LINK TO DIAGRAM
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi anees420 and welcome to the board.

That shouldn't be too difficult to describe. If I follow your diagram correctly, basically, you want all rows delete where the value in column G is greater than 100,000 or equal to 0.

The following assumes you have field names / column labels in row 1.

Code:
[COLOR=blue]Public[/COLOR] [COLOR=blue]Sub[/COLOR] DelRows()
    [COLOR=blue]Dim[/COLOR] rngValues [COLOR=blue]As[/COLOR] Range
    [COLOR=blue]Const[/COLOR] lngCriteria1 [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] = 100000
    [COLOR=blue]Const[/COLOR] lngCriteria2 [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] = 0
 
    [COLOR=blue]Set[/COLOR] rngValues = Range(Cells(1, 8), Cells(Rows.Count, 8).End(xlUp))
 
    ActiveSheet.AutoFilterMode = [COLOR=blue]False[/COLOR]
    [COLOR=blue]With[/COLOR] rngValues
        .AutoFilter field:=1, Criteria1:=">" & lngCriteria1, _
            Operator:=xlOr, Criteria2:=lngCriteria2
        .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]

Edit: Changed to column H. Revisitng your diagram, you appear to want the crietria applied to column H. You seem to have the same condition, regardless of whether or not column G is between 4000 and 100000.
 
Last edited:
Upvote 0
Thank you for the quick reply.... :)

but the macro did not work

---Trust me the Diagram explains it---

HELP HELP
 
Upvote 0
You diagram suggests the following:

If value in column G is between 40000 and 100000 Then
If value in column H is 0 Then delete the row.
If value in column H is greater than 100000 Then delete the row.

If value in column G is not between 40000 and 100000 Then
If value in column H is 0 Then delete the row.
If value in column H is greater than 100000 Then delete the row.

Why is it important to test whether or not the value in column G is within 40000 and 100000 if the resulting action is the same in both instances?

but the macro did not work
Care to elaborate?
 
Upvote 0
If value in column G is between 40000 and 100000 Then
If value in column H is 0 Then delete the row. <---- KEEP ROW
If value in column H is greater than 100000 Then delete the row.

If value in column G is not between 40000 and 100000 Then
If value in column H is 0 Then delete the row.
If value in column H is greater than 100000 Then delete the row.


Hope that helps... Thank you again
 
Upvote 0
G1 = 120,000
H1 = 90,000
Keep Row

G2 = 90,000
H2 = 0
Keep Row

G3 = 80,000
H3 = 95,000
keep Row

----------------------

G4 = 90,000
H4 = 105,000
Delete Row

G5 = 140,000
H5 = 0
Delete Row

G6 = 155,000
H6 = 206,000
Delete Row


---- Maybe you can try these values--- these are random numbers... i hope this helps... thank you
 
Upvote 0
I seem to have overlooked the 2nd rule. Try this:

Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] DeleteRows()
    [COLOR="Blue"]Dim[/COLOR] rngValues [COLOR="Blue"]As[/COLOR] Range
    [COLOR="Blue"]Dim[/COLOR] rngDelete [COLOR="Blue"]As[/COLOR] Range
    
    [COLOR="Blue"]Set[/COLOR] rngValues = Range(Cells(1, 7), Cells(Rows.Count, 8).End(xlUp))
    
    ActiveSheet.AutoFilterMode = [COLOR="Blue"]False[/COLOR]
    [COLOR="Blue"]With[/COLOR] rngValues
        .AutoFilter field:=1, Criteria1:=">=" & 40000, Operator:=xlAnd, Criteria2:="<=" & 100000
        .AutoFilter field:=2, Criteria1:=">" & 100000
        [COLOR="Blue"]Set[/COLOR] rngDelete = .Offset(1).SpecialCells(xlCellTypeVisible)
        .AutoFilter field:=1, Criteria1:="<" & 40000, Operator:=xlOr, Criteria2:=">" & 100000
        .AutoFilter field:=2, Criteria1:="=" & 0, Operator:=xlOr, Criteria2:=">" & 100000
        [COLOR="Blue"]Set[/COLOR] rngDelete = Union(rngDelete, .Offset(1).SpecialCells(xlCellTypeVisible))
        .AutoFilter
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
    rngDelete.EntireRow.Delete
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
----- FANTASTIC ---- the macro works GREAT !!!! THANK YOU :)

if its not to much trouble can it also be sorted out

ascending from column H then G

&

Run the macro in a loop every 30 seconds

also how would i be able to stop it once its going without closing down excel or the workbook
 
Upvote 0
Try;

Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] DeleteRows()
    [COLOR="Blue"]Dim[/COLOR] rngValues [COLOR="Blue"]As[/COLOR] Range
    [COLOR="Blue"]Dim[/COLOR] rngDelete [COLOR="Blue"]As[/COLOR] Range
    
    [COLOR="Blue"]Set[/COLOR] rngValues = Range(Cells(1, 7), Cells(Rows.Count, 8).End(xlUp))
    
    ActiveSheet.AutoFilterMode = [COLOR="Blue"]False[/COLOR]
    [COLOR="Blue"]With[/COLOR] rngValues
        .AutoFilter field:=1, Criteria1:=">=" & 40000, Operator:=xlAnd, Criteria2:="<=" & 100000
        .AutoFilter field:=2, Criteria1:=">" & 100000
        [COLOR="Blue"]Set[/COLOR] rngDelete = .Offset(1).SpecialCells(xlCellTypeVisible)
        .AutoFilter field:=1, Criteria1:="<" & 40000, Operator:=xlOr, Criteria2:=">" & 100000
        .AutoFilter field:=2, Criteria1:="=" & 0, Operator:=xlOr, Criteria2:=">" & 100000
        [COLOR="Blue"]Set[/COLOR] rngDelete = Union(rngDelete, .Offset(1).SpecialCells(xlCellTypeVisible))
        .AutoFilter
        .Sort key1:=Range("H1"), key2:=Range("G1"), header:=xlYes
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
    rngDelete.EntireRow.Delete
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,666
Messages
6,120,806
Members
448,990
Latest member
rohitsomani

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