• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Using Multiple IF Conditions in VBA Macro

Vijaychitra

New Member
Hello,

Tried to use the below IF conditions in excel, I'm able to get the required data in the column. However when I tried to add the same condition in VB MACRO with multiple IF conditions, the macro throws an compilation error.

In EXCEL
=IF(ISNUMBER(SEARCH("APPLE",J4)),"APPLE", IF(ISNUMBER(SEARCH("MAT",J4)),"MAT",IF(ISNUMBER(SEARCH("CAT",J4)),"CAT"))) - Works
In MACRO

Range("B2:B" & last_row).Formula = =IF(ISNUMBER(SEARCH("APPLE",J4)),"APPLE", IF(ISNUMBER(SEARCH("MAT",J4)),"MAT",IF(ISNUMBER(SEARCH("CAT",J4)),"CAT")))
It throws a completion error.

In EXCEL

=IF(COUNT(SEARCH("APPLE",J4)),"APPLE", IF(COUNT(SEARCH("MAT",J4)),"MAT",IF(COUNT(SEARCH("CAT",J4)),"CAT")))- Works
In MACRO

Range("B2:B" & last_row).Formula = IF(COUNT(SEARCH("APPLE",J2)),"APPLE"),IF(COUNT(SEARCH("MAT",J2)),"MAT"),IF(COUNT(SEARCH("CAP",J2)),"CAP"))))
It throws a completion error.

Let me know how the above 2 IF conditions can be incorporated in VB MACRO with multiple IF conditions?

Thanks...
 
Hello, you just forgot some double quotes :​
Range("B2:B" & last_row).Formula = "=IF(ISNUMBER(SEARCH(""APPLE"",J4)),""APPLE"",IF(ISNUMBER(SEARCH(""MAT"",J4)),""MAT"",IF(ISNUMBER(SEARCH(""CAT"",J4)),""CAT"")))"
 
Back
Top