Excel VBA: multiple if cell contains string X then make cell on left value Y

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Hello,

How would I go about VBA code for multiple:

if cell in Range("C10:C27") contains string X then make cell on left value Y
if cell in Range("C10:C27") contains string X1 then make cell on left value Y1
if cell in Range("C10:C27") contains string X2 then make cell on left value Y2
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You want to use the function InStr([start_pos], string1, string2). This returns the position of string2 within string1, and will return 0 if string2 is not included in string1. This way, you can check if InStr returns a value greater than 0 to see if it's contained within, as follows.
VBA Code:
Sub SetLeftIfContains(ByVal InputCells As Range, ByVal CheckIfContainsString As String, ByVal OutputString As String)

    Dim c As Range
    For Each c In InputCells.Cells
  
        If InStr(c.Value, CheckIfContainsString) > 0 Then Cells(c.Row, c.Column - 1).Value = OutputString
      
    Next c
  
End Sub

You can then use this method on your examples as follows:
VBA Code:
SetLeftIfContains Range("C10:C27"), X, Y
SetLeftIfContains Range("C10:C27"), X1, Y1
SetLeftIfContains Range("C10:C27"), X2, Y2

You can replace Range("C10:C27") with Selection and it'll operate on all selected cells
 
Upvote 0
Solution
You want to use the function InStr([start_pos], string1, string2). This returns the position of string2 within string1, and will return 0 if string2 is not included in string1. This way, you can check if InStr returns a value greater than 0 to see if it's contained within, as follows.
VBA Code:
Sub SetLeftIfContains(ByVal InputCells As Range, ByVal CheckIfContainsString As String, ByVal OutputString As String)

    Dim c As Range
    For Each c In InputCells.Cells
 
        If InStr(c.Value, CheckIfContainsString) > 0 Then Cells(c.Row, c.Column - 1).Value = OutputString
     
    Next c
 
End Sub

You can then use this method on your examples as follows:
VBA Code:
SetLeftIfContains Range("C10:C27"), X, Y
SetLeftIfContains Range("C10:C27"), X1, Y1
SetLeftIfContains Range("C10:C27"), X2, Y2

You can replace Range("C10:C27") with Selection and it'll operate on all selected cells
Where do I put those last three lines of code?
 
Upvote 0
You can put them in their own sub. Just replace the X, Y, X1, Y1, X2, and Y2 values with the strings you actually want there, enclosed by quotes.
Excel Formula:
Sub SetLeftMacro()
    SetLeftIfContains Range("C10:C27"), X, Y
    SetLeftIfContains Range("C10:C27"), X1, Y1
    SetLeftIfContains Range("C10:C27"), X2, Y2
End Sub
then you can run this macro from the macros window
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top