Link to home
Start Free TrialLog in
Avatar of mldaigle1
mldaigle1Flag for Canada

asked on

Excel Macro for If condition

Hi,

Here my need:

for each row of the table, verify
              if  
                       (Cell Gxx (which is the manufacturer) the value is equal to "VMware inc" or "Microsoft Corporation"
                           or
                        Cell Uxx is equal to lowercase "trader")
                  then write value "Yes" in Cell XX (which is the cell of last column of the table)
                  else write value "No" in Cell XX (which is the cell of last column of the table)
             
Does it sound clear?
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Do you truly need a macro, or would a formula work?
=IF(OR(G2="VMware inc", G2="Microsoft Corporation", NOT(ISERR(FIND("trader", U2)))), "Yes", "No")

Open in new window

Avatar of Rgonzo1971
Rgonzo1971

HI,

pls try

Sub Macro()
LastCol = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    If Range("G" & c.Row) = "VMware Inc" Or _
            Range("G" & c.Row) = "Microsoft Corporation" Or _
            LCase(Range("U" & c.Row)) = "trader" Then
        Cells(c.Row, LastCol) = "Yes"
    Else
        Cells(c.Row, LastCol) = "No"
    End If
Next
End Sub

Open in new window

Regards
No macro, just a simple formula

=IF(OR(OR(G1="Microsoft",G1="Vmware"),U1="Trader"),"YES","No")

Open in new window

Noticed I missed the lower case requirement in my formula:
=IF(OR(G2="VMware inc", G2="Microsoft Corporation", NOT(ISERR(FIND("trader", LOWER(U2))))), "Yes", "No")

Open in new window

Oops, capital letter!!

=IF(OR(OR(G1="Microsoft Corporation",G1="VMware inc"),U1="trader"),"YES","No")

Open in new window

Perhaps this formula in the next empt column copied down.

=IF(OR(G2="VMware inc", G2="Microsoft Corporation", U2="trader"), "Yes", "No")

Which in code would be this.
Dim rng As Range

    With Sheets("Sheet1")    ' change Sheet1 to relevant sheet
        ' find next empty column  in first row
        Set rng = .Cells(1, .Columns.Count).End(xlToLeft).Offset(, 1)    
        Set rng = rng.Resize(.Range("G" & .Rows.Count).End(xlUp).Row - 1)
        With rng
            .Formula = "=IF(OR(G2=""VMware inc"", G2=""Microsoft Corporation"", U2=""trader""), ""Yes"", ""No"")"
            .Value = .Value
        End With
    End With

Open in new window

Avatar of mldaigle1

ASKER

Thanks guys for your help but i really needed a macro since i will chain it with other macros.

imnorie,
your macro provide me a result, but not what i expected.


I optimize the macro as follow:

Dim rng As Range
Sub Exempt()
    With Sheets("Table")
        ' find next empty column  in second row
        Set rng = .Cells(2, .Columns.Count).End(xlToLeft).Offset(, 1)
        Set rng = rng.Resize(.Range("G" & .Rows.Count).End(xlUp).Row - 1)
        With rng
            .Formula = "=IF(OR(G2=""VMware, Inc."", G2=""Microsoft Corporation"", U2=""Yes""), ""Yes"", ""No"")"
            .Value = .Value
        End With
    End With
End Sub


but it only worked for U2=Yes, and totally ignore the manufacturer and wrote as result "#N/A" and not "No".....
I can't see how the code I posted could produce results like that.

Do you have any error values in column G or column U?

Could you attach a small sample workbook?

By the way, why did you use .Cells(2, Columns.Count) instead of Cells(1, Columns.Count)?

Also why did you change U2="trader" to U2="Yes"?
The question seems to be changing...
cell1 is my header
i modify the value from trader to yes in the original report, so i modify it in the formula as well
sample attach as requested
thanks for your help
sample.txt
Can you upload a workbook rather than a text file?:)
of course

:)
sample.xlsx
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, works like a charm

Happy New Year!

:)

I now have a complete table to play with in order to provide stats to my boss.  
Thanks again everybody!