SolvedPrivate

VBA excel condition

Posted on 2015-01-19
16
14 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
16 Comments
 
LVL 5

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 5

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
 

Author Comment

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 25

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 25

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 500 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 45

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now