?
Solved

A few If Statements

Posted on 2013-05-10
9
Medium Priority
?
261 Views
Last Modified: 2013-05-14
I am terrible with if statements, and have a need a sub with 4.

On active tab and starting in row 3.

If column E and column AJ equal each other, move to the next row do nothing.

If column E has anything in it and column AJ is blank, of that row turn column C thru E green.

If column E is blank and column AJ has anything in it, of that row turn column AG thru AJ red.

If any red cells (column AJ) = green cells (column E) , delete the green row, and turn the red one yellow.  Need to do a top down search of column E

If column E and column AJ are both blank?  Home cursor and stop sub.




Looking assistance i writing this sub..  Thanks. -R-
0
Comment
Question by:RWayneH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 39156926
Here is a macro that does what I think you requested.
Sub EversusAJ()
Dim celG As Range, celR As Range, rg As Range, rgGreen As Range, rgRed As Range, rw As Range
With ActiveSheet
    Set rg = .UsedRange
    Set rg = Range(.Cells(3, 1), rg.Cells(rg.Rows.Count, rg.Columns.Count))
End With
rg.Interior.ColorIndex = -4142  'No highlighting
For Each rw In rg.Rows
    If (rw.Cells(1, "E") = "") And rw.Cells(1, "AJ") = "" Then
        Exit For
    ElseIf rw.Cells(1, "E") = "" Then
        rw.Cells(1, "AG").Resize(1, 4).Interior.ColorIndex = 3      'red
        Set rgRed = rw.Cells(1, "AG").Resize(1, 4)
        For Each celR In rgRed.Cells
            If celR <> "" Then
                Set celG = Nothing
                On Error Resume Next
                Set celG = rgGreen.Find(celR.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
                On Error GoTo 0
                If Not celG Is Nothing Then
                    celG.EntireRow.Delete
                    celR.EntireRow.Cells(1, "AG").Resize(1, 4).Interior.ColorIndex = 6   'Yellow
                End If
            End If
        Next
    ElseIf rw.Cells(1, "AJ") = "" Then
        rw.Cells(1, "C").Resize(1, 3).Interior.ColorIndex = 14       'green
        If rgGreen Is Nothing Then
            Set rgGreen = rw.Cells(1, "C").Resize(1, 3)
        Else
            Set rgGreen = Union(rgGreen, rw.Cells(1, "C").Resize(1, 3))
        End If
    ElseIf rw.Cells(1, "E") = rw.Cells(1, "AJ") Then
        
    End If
Next

End Sub

Open in new window

Brad
0
 

Author Comment

by:RWayneH
ID: 39157034
Thanks Brad.
I did make a bad mistake...  I do not want to delete, the green.

In the statement...
If any red cells (column AJ) = green cells (column E) , delete the green row, and turn the red one yellow.  Need to do a top down search of column E

can we turn them both yellow?  I need to look at them before I delete them. -R-
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 39157092
Here is the code with the requested changes. It is now searching only green values in column E that occur above red values in column AJ. If it finds any, it turns the matching cells in columns E and AJ yellow--while leaving the remainder of those rows green and red.
Sub EversusAJ()
Dim celG As Range, celR As Range, rg As Range, rgGreen As Range, rw As Range
With ActiveSheet
    Set rg = .UsedRange
    Set rg = Range(.Cells(3, 1), rg.Cells(rg.Rows.Count, rg.Columns.Count))
End With
rg.Interior.ColorIndex = -4142  'No highlighting
For Each rw In rg.Rows
    If (rw.Cells(1, "E") = "") And rw.Cells(1, "AJ") = "" Then
        Exit For
    ElseIf rw.Cells(1, "E") = "" Then
        Set celR = rw.Cells(1, "AJ")
        rw.Cells(1, "AG").Resize(1, 4).Interior.ColorIndex = 3      'red
        If celR <> "" Then
            Set celG = Nothing
            On Error Resume Next
            Set celG = rgGreen.Find(celR.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
            On Error GoTo 0
            If Not celG Is Nothing Then
                celR.Interior.ColorIndex = 6   'Yellow
                celG.Interior.ColorIndex = 6   'Yellow
            End If
        End If
    ElseIf rw.Cells(1, "AJ") = "" Then
        rw.Cells(1, "C").Resize(1, 3).Interior.ColorIndex = 14       'green
        If rgGreen Is Nothing Then
            Set rgGreen = rw.Cells(1, "E")
        Else
            Set rgGreen = Union(rgGreen, rw.Cells(1, "E"))
        End If
    ElseIf rw.Cells(1, "E") = rw.Cells(1, "AJ") Then    'Do nothing
        
    End If
Next
End Sub

Open in new window

Brad
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RWayneH
ID: 39157119
Is there a way to just search the green cells for match reds?  There are plenty that will match that are not going to be green or red.  I would like those the remain unformatted (no color).  -R-
0
 
LVL 81

Expert Comment

by:byundt
ID: 39157157
What you requested is the way the code is designed (and working) in my test workbook.

Do note that the search for green cells only considers those that lie above the red cell. That's the way your original question was written--but it may not be what you really want. Please advise if the code needs to change.
0
 

Author Comment

by:RWayneH
ID: 39162207
Thanks I was not able to test til today.  EXCELent!!  Thanks. -R-
0
 

Author Closing Comment

by:RWayneH
ID: 39162212
Thanks. -R-
0
 

Author Comment

by:RWayneH
ID: 39164813
Is there a way that expand on this or do I need to submit another question?

Start in row of E4 of active tab, check and do the following:

1.      If yellow leave the row alone and move to the next row down E5 and check that.
2.      If blank leave alone, (there can be multiple blanks in a row),
a.      Look for next value in column E to check
b.      If none there Exit Sub.
3.      If cell in column E is green, and the cell in the same row (Column AJ) is blank.
a.      Copy column C thru I of that row, to AG  out.
b.      If column AJ is not blank.. move to the next row to check in column E.
0
 
LVL 81

Expert Comment

by:byundt
ID: 39164922
I think what you are describing now is best suited for a new question.

When you post it, the color aspect needs to be clarified. Ideally, you would post a sample workbook with a few rows of data showing both yellow and green colors. If this is not possible, then you will need to specify:
1.  What ColorIndex or RGB colors did you use for yellow and green?
2.  Are the cells yellow or green because of Conditional Formatting? If so, what is the criteria?
3.  Are the cells are yellow or green because of regular formatting?
4.  Which version of Excel are you using?

Excel 2010 added the DisplayFormat property, which circumvents the discussion about Conditional Formatting/Regular Formatting by returning the ColorIndex or RGB color that is actually being displayed.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question