Learn how to a build a cloud-first strategyRegister Now

x
?
SolvedPrivate

VBA excel condition

Posted on 2015-01-19
16
Medium Priority
?
31 Views
Last Modified: 2016-02-10
I want to compare one column to another column, for example if in column A I have "Toyota" then in Column D I should have "Corolla" if not then highlight both cells with red color, I want to put multiple conditions like that for different columns. how I can do that through VBA? i have thousand of records.
thanks
0
Comment
Question by:snhandle
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 6

Expert Comment

by:Flora
ID: 40558988
assuming that you have your data like Toyota and Corolla somewhere in the sheet.

lets assume that your conditional data is in F and G
so if A Toyota is not equal to F value and D Corolla is not equal to G value then it will trigger red background. check it in the attached file.  add more data and you will see the highlight is automatic
Book3.xlsx
0
 

Author Comment

by:snhandle
ID: 40559008
You are right, but i want to trigger red only column A cell and D cell not the whole row. Also, I want to have multiple conditions, like instead of Toyota and Corolla if we have "Cell phone" in column A and "Bill" in Column D then it should be also ok otherwise it should trigger the red background.
Thanks
0
 
LVL 6

Expert Comment

by:Flora
ID: 40559148
But you need to have your conditional data somewhere I mean  list of cellphone etc name against which the check on cells need to happen.  You need to put those in F and G or anywhere you like but the data from which the column A and D to be tested must exist somewhere, otherwise excel wouldn't know to check cells of A and D against what data?

I will revise the workbook range that it only colours the two columns if mismatch not the entire row .
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:snhandle
ID: 40559761
Yes we can put the conditional data in sheet2.
0
 
LVL 27

Expert Comment

by:ProfessorJimJam
ID: 40560111
Hi Snhandle,

i dont want to step in Flora's toes here, but thought to quickly answer.  

please see attached.   the conditional data are in sheet2  Column A and Column B.
Book3.xlsx
0
 

Author Comment

by:snhandle
ID: 40561262
But I want to highlight the  cells A and D, not the whole row. Also, I want to put multiple condition in sheet two, if cell A and D matches any of those condition then it should not make cell A and D in sheet one red.
0
 

Author Comment

by:snhandle
ID: 40565768
Any help?
0
 
LVL 27

Expert Comment

by:ProfessorJimJam
ID: 40565984
Snhandle

please see attached. only highlights A and D
Book3.xlsx
0
 

Author Comment

by:snhandle
ID: 40567762
where can i find the VBA script? When I go to VBA, no script is there.
0
 

Author Comment

by:snhandle
ID: 40567783
I want to see the script so I can modify it according to my needs. Also, I need to highlights those cell only when in the sheet2 there is no match in the column A and B not just the cells.
0
 
LVL 27

Expert Comment

by:ProfessorJimJam
ID: 40567937
Look, when you can do things with build in functions of excel, there is no need for VBA. Vba will make your spreadsheet slow and you would not know modify the code , unless you know vba language . Check the last workbook I attached then check the conditional formatting  and see the range and formula inside conditional formatting, then you would be able to modify it according your need..
0
 
LVL 5

Accepted Solution

by:
Rodney Endriga earned 2000 total points
ID: 40608914
Snhandle, here is some VBA code you can use to start your search:

Sub EE_CellCheckCode()
Dim rng1 As Range, rng2 As Range
Set rng1 = ActiveSheet.Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
Set rng2 = ActiveSheet.Range("D1:D" & ActiveSheet.UsedRange.Rows.Count)

For Each cell In rng1
     cell.Select
     If cell.Value <> "Toyota" Then  'Checks for TOYOTA in the cell (ColumnA)
         With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
         End With
     Else
        With Selection.Interior
           .Pattern = xlNone
           .TintAndShade = 0
           .PatternTintAndShade = 0
        End With
     End If
Next

For Each cell In rng2
     cell.Select
     If cell.Value <> "Corolla" Then   'Checks for COROLLAin the cell (ColumnD)
         With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
         End With
     Else
        With Selection.Interior
           .Pattern = xlNone
           .TintAndShade = 0
           .PatternTintAndShade = 0
        End With
     End If
Next

End Sub
0
 

Author Comment

by:snhandle
ID: 40611568
The above VBA working fine the only thing is it does not highlight the column A cell in case the condition is not true, it only highlight column D cell if the condition is not true. Also, how i can add multiple conditions into this statement like if column A has mercedez then column d should have benz and so on.
Thanks
0
 
LVL 5

Expert Comment

by:Rodney Endriga
ID: 40629729
Hi snhandle,

Maybe something like this would work better for you:
(you will be able to add the car combinations you would like in the IF statement)

Sub EE_CellCheckCode()
Dim index As Integer
Dim ws As Worksheet

'set the sheet to use
Set ws = Sheets("Sheet3")

    For index = 1 To ActiveSheet.UsedRange.Rows.Count

    'Checks if cells in Column A & Column D MATCH the specified combination
        If ws.Range("A" & index).Value = "Toyota" And ws.Range("D" & index).Value = "Corolla" Or _
            ws.Range("A" & index).Value = "Toyota" And ws.Range("D" & index).Value = "Tacoma" Or _
            ws.Range("A" & index).Value = "Toyota" And ws.Range("D" & index).Value = "Tundra" Or _
            ws.Range("A" & index).Value = "Mercedes" And ws.Range("D" & index).Value = "Benz" Or _
            ws.Range("A" & index).Value = "Honda" And ws.Range("D" & index).Value = "Fit" Or _
            ws.Range("A" & index).Value = "Honda" And ws.Range("D" & index).Value = "Civic" Or _
            ws.Range("A" & index).Value = "Honda" And ws.Range("D" & index).Value = "Accord" Then
            'DOES NOT Fill any color if both cell values MEET the above conditions
            ws.Range("A" & index).Interior.Color = xlNone
            ws.Range("D" & index).Interior.Color = xlNone
        Else
            'Highlights both cell values RED (if FALSE; criteria DOES NOT MEET above conditions)
            ws.Range("A" & index).Interior.Color = vbRed
            ws.Range("D" & index).Interior.Color = vbRed
        End If

    Next index

End Sub
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 40684211
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

810 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