[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 32
  • Last Modified:

VBA excel condition

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
snhandle
Asked:
snhandle
  • 7
  • 3
  • 2
  • +2
1 Solution
 
FloraCommented:
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
 
snhandleAuthor Commented:
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
 
FloraCommented:
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
snhandleAuthor Commented:
Yes we can put the conditional data in sheet2.
0
 
ProfessorJimJamCommented:
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
 
snhandleAuthor Commented:
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
 
snhandleAuthor Commented:
Any help?
0
 
ProfessorJimJamCommented:
Snhandle

please see attached. only highlights A and D
Book3.xlsx
0
 
snhandleAuthor Commented:
where can i find the VBA script? When I go to VBA, no script is there.
0
 
snhandleAuthor Commented:
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
 
ProfessorJimJamCommented:
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
 
Rodney EndrigaCommented:
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
 
snhandleAuthor Commented:
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
 
Rodney EndrigaCommented:
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
 
Martin LissOlder than dirtCommented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now