Evaluate IF(AND is erasing all values

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Trying to get Evaluate(IF(AND to work.

Code that works:
VBA Code:
Sub TestEvaluateIF()
'
' Check values in A1:A10
'
    With Range(Cells(1, 1), Cells(10, 1))
        .Value = Evaluate("IF(" & .Address & "= ""Good"", ""OK""," & .Address & ")")        '   If cell in Column A range = 'Good' then change Column A
'                                                                                           '           cell to 'OK' Else leave the value as is
    End With
End Sub


Code that erases all values from Column A:
VBA Code:
Sub TestEvaluateIF_And()
'
' Check values in A1:B10 via offset ... This erases all values from Column A for some reason?
'
    With Range(Cells(1, 1), Cells(10, 1))
        .Value = Evaluate("IF(AND(" & .Address & "= ""Definitely"", " & _
                .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")             '   If cell in Column A range = 'Definitely' & cell
'                                                                                           '           in Column B range = 'Good' then change
'                                                                                           '           Column A cell to 'OK' Else leave the value as is
    End With
End Sub


Book1
ABC
1DefinitelyGood
2DefinitelyGood
3MaybeBad
4MaybeGood
5MaybeBad
6DefinitelyGood
7DefinitelyBAD
8MaybeBAD
9DefinitelyGood
10DefinitelyGood
11
Sheet1


Before I smash this computer, Please let me know what I am messing up.
 
Steve. Can you help me to learn Evaluate
It appears to me the script looks for the Value "Good" in Range("A1:A10")
If it finds "Good" it changes the value to "OK"

But when I run your script or the other script it does not work.

Actually the 'Good' is checked in Column B. ;) There is an offset(, 1) in the 2nd script (If(AND)) Which makes it column B. ;) Please read the comments in my latest post.
 
Last edited:
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe these 2 versions of code ... translated from Cells to just Range makes more sense:

VBA Code:
Sub TestEvaluateIFV2A()
'
' Check values in B1:B10
'
    With Range("B1:B10")
        .Value = Evaluate("IF(" & .Address & "= ""Good"", ""OK""," & .Address & ")")        '   If cell in Column B range = 'Good' then change Column B
'                                                                                           '           cell to 'OK' Else leave the value as is
    End With
End Sub


Sub TestEvaluateIF_AND_V2A()
'
' Check values in A1:B10    AND needs to be replaced with '*' ;)
'
    With Range("A1:A10")
        .Value = Evaluate("IF((" & .Address & "= ""Definitely""" & ")*(" & _
                .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")             '   If cell in Column A range = 'Definitely' & cell
'                                                                                           '           in Column B range = 'Good' then change
'                                                                                           '           Column A cell to 'OK' Else leave the value as is
    End With
End Sub
 
Upvote 0
@My Aswer Is This Let me repost all the working Data/code for you.

Book1
ABC
1DefinitelyGood
2DefinitelyGood
3MaybeBad
4MaybeGood
5MaybeBad
6DefinitelyGood
7DefinitelyBAD
8MaybeBAD
9DefinitelyGood
10DefinitelyGood
11
Sheet1


VBA Code:
Sub TestEvaluateIFV2()
'
' Check values in B1:B10
'
    With Range(Cells(1, 2), Cells(10, 2))
        .Value = Evaluate("IF(" & .Address & "= ""Good"", ""OK""," & .Address & ")")        '   If cell in Column B range = 'Good' then change Column B
'                                                                                           '           cell to 'OK' Else leave the value as is
    End With
End Sub


VBA Code:
Sub TestEvaluateIF_AND_V2()
'
' Check values in A1:B10    AND needs to be replaced with '*' ;)
'
    With Range(Cells(1, 1), Cells(10, 1))
        .Value = Evaluate("IF((" & .Address & "= ""Definitely""" & ")*(" & _
                .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")             '   If cell in Column A range = 'Definitely' & cell
'                                                                                           '           in Column B range = 'Good' then change
'                                                                                           '           Column A cell to 'OK' Else leave the value as is
    End With
End Sub

Let us know if those codes don't work for you.
The first one worked but not the second one. Thanks
 
Upvote 0
What do you mean when you say second one didn't work? Are you saying column A didn't change at all, it erased all of column A?
Didn't work doesn't really describe what happened or didn't happen.
 
Upvote 0
OK This script worked:
VBA Code:
Sub TestEvaluateIFV2A()
'
' Check values in B1:B10
'
    With Range("B1:B10")
        .Value = Evaluate("IF(" & .Address & "= ""Good"", ""OK""," & .Address & ")")        '   If cell in Column B range = 'Good' then change Column B
'                                                                                           '           cell to 'OK' Else leave the value as is
    End With
End Sub
 
Upvote 0
That is the same as the other one you said worked, Only change was from Range(Cells to just Range.
 
Upvote 0
OK this script worked:
VBA Code:
Sub TestEvaluateIF_AND_V2A()
'
' Check values in A1:B10    AND needs to be replaced with '*' ;)
'
    With Range("A1:A10")
        .Value = Evaluate("IF((" & .Address & "= ""Definitely""" & ")*(" & _
                .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")             '   If cell in Column A range = 'Definitely' & cell
'                                                                                           '           in Column B range = 'Good' then change
'                                                                                           '           Column A cell to 'OK' Else leave the value as is
    End With
End Sub
I now see we are looking at column A and B
A Must have "Definitely"
And B must Have "Good"

I did not do that first time:
Thanks for all your help
 
Upvote 0
Yes. The IF(AND script looks at Columns A & B and changes Column A when conditions are met.
The IF script looks at Column B & changes Column B when condition is met.
 
Upvote 0
Yes. The IF(AND script looks at Columns A & B and changes Column A when conditions are met.
The IF script looks at Column B & changes Column B when condition is met.
Thanks I learned one new thing here today.
Never have used Evaluate before today
Take care
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,665
Members
449,462
Latest member
Chislobog

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