VBA to change cell value based on multiple conditions

tonyc527

New Member
Joined
Mar 8, 2016
Messages
16
Hey,

I'm looking for some help with this

I would like to set the value of any cell in column D to "Yellow789" where that rows column A value begins with (but may contain much more than) "red123", column B value contains (but may not begin or end with) "blue456", and Column C value is blank.

Cheers!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Welcome to the Board!

You don't need VBA to do this, you could just use the formula (in cell D1 and copy down):
Code:
=IF(AND(LEFT(A1,6)="red123",LEFT(B1,7)="blue456",C1=""),"Yellow789","")
 
Upvote 0
If you really want a VBA solution to it, this will do that:
Code:
Sub MyPopulateMacro()

    Dim myLastRow As Long
    Dim myRow As Long
    
'   Find last row with data in column A
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through data
    For myRow = 1 To myLastRow
        If (Left(Cells(myRow, "A"), 6) = "red123") _
                 And (Left(Cells(myRow, "B"), 7) = "blue456") _
                 And (Cells(myRow, "C") = "") Then
            Cells(myRow, "D") = "Yellow789"
        End If
    Next myRow
    
End Sub
 
Last edited:
Upvote 0
You can test this out.
It goes in the sheet module which means you have to right click on the sheet tab and choose view code, then paste the code into the white window.
Make sure you have macros enabled.
This could probably be done better, maybe a regular expression, but it's the best I can do.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Columns("A:C")) Is Nothing Then Exit Sub
Application.EnableEvents = False
    If Len(Cells(Target.Row, 2)) < 7 Then
        Cells(Target.Row, 4).Value = Empty
        Application.EnableEvents = True
        Exit Sub
    End If
    If Cells(Target.Row, 1).Value Like "red123*" And _
        Not "blue456" Like Right(Cells(Target.Row, 2).Value, 7) And _
        Not "blue456" Like Left(Cells(Target.Row, 2).Value, 7) And _
        Cells(Target.Row, 2).Value Like "*?blue456?*" And _
        Cells(Target.Row, 3).Value = Empty Then
        Cells(Target.Row, 4).Value = "Yellow789"
    Else
        Cells(Target.Row, 4).Value = Empty
    End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks Joe. The challenge with just using a formula would be that users would paste data into a sheet where column d may contain values, but it will not contain values when the original conditions are met. I would like those records to be categorized in some way.

Will your and(left(cells function grab any record that contains the value specified or is that just going to look for a value that begins with the value specified? If the latter, could you help me with finding the record where the b column contains "blue456"?

Thanks again
 
Upvote 0
Try this variation, in which we can use the InStr function instead of Left on column B (see: MS Excel: How to use the INSTR Function (VBA)
Code:
Sub MyPopulateMacro()

    Dim myLastRow As Long
    Dim myRow As Long
    
'   Find last row with data in column A
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through data
    For myRow = 1 To myLastRow
        If (Left(Cells(myRow, "A"), 6) = "red123") _
                And (InStr(Cells(myRow, "B"), "blue456") > 0) _
                And (Cells(myRow, "C") = "") Then
            Cells(myRow, "D") = "Yellow789"
        End If
    Next myRow
    
End Sub
 
Upvote 0
So what happened with this?
column A value begins with (but may contain much more than) "red123"
column B value contains (but may not begin or end with)"blue456"

When I type "magicblue456" into column B, then my code rejects is because it ends in "blue456", but Joe's code accepts it, so you didn't want that criteria after all?
 
Last edited:
Upvote 0
When I type "magicblue456" into column B, then my code rejects is because it ends in "blue456", but Joe's code accepts it, so you didn't want that criteria after all?
Hmm... good point. I missed that part originally (trying to do too many things at once here).
You could modify my IF clause like this:
Code:
        If (Left(Cells(myRow, "A"), 6) = "red123") _
                And (InStr(Cells(myRow, "B"), "blue456") > 0) _
[COLOR=#ff0000]                And (Right(Cells(myRow, "B"), 7) <> "blue456") _[/COLOR]
                And (Cells(myRow, "C") = "") Then
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,803
Members
449,337
Latest member
BBV123

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