OzGrid

How to use VBA code for multiple IF conditions

< Back to Search results

 Category: [Excel]  Demo Available 

How to use VBA code for multiple IF conditions

 

Requirement:

 

The user wants to add one Macro to check text value from Column A (which has short text) and if add full text in Column M.

Example: A2 has "ON" and then in M2 should give "Ontario"

Formula:
=IF(
ISNUMBER(SEARCH("ON",A2)),"Ontario",
IF(ISNUMBER(SEARCH("BC",A2)),"British Columbia",
IF(ISNUMBER(SEARCH("QC",A2)),"Qubec"))

 

https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/1203080-vba-code-for-multiple-if-conditions

 

Solution:

 

Code:
Option Explicit

Sub Province()
    Dim lr As Long, i As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
        If Range("A" & i) = "ON" Then
            Range("M" & i) = "Ontario"
        ElseIf Range("A" & i) = "BC" Then
            Range("M" & i) = "British Columbia"
        ElseIf Range("A" & i) = "QC" Then
            Range("M" & i) = "Quebec"
        End If
    Next i
End Sub

 

Obtained from the OzGrid Help Forum.

Solution provided by AlanSidman.

 

See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets

 

See also:

How to use VBA code to select if cell contains any text return text in another cell
How to display a message for each if the value is greater than the mentioned values
How to remove text after last comma, only if cell has 3 commas
How to delete rows if cell doesn't contain criteria

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.

 

 


Gallery



stars (0 Reviews)