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.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Please do not do that.
Before I smash this computer, Please let me know what I am messing up

I can write you a script to do what you want but I never use Evaluate
Not sure I understand it

If doing what you want is Ok to do my way tell me in words what you attempting to do.
I do not understand the script your showing.
Doing it my way may take 5 more seconds then using Evaluate if you can spare a few more seconds.
Depending if your using 800 rows or 800 thousand rows
 
Upvote 0
Thank you for responding @My Aswer Is This.

I do not understand the script your showing.

I left comments to the right of the code to clearly explain what my intentions are. The first code works as I intended. The 2nd code doesn't.

I can write you a script to do what you want but I never use Evaluate
Not sure I understand it

I am certainly not a pro on 'Evaluate'. From my understanding, it is a way to process a worksheet formula in VBA without having to write the formula to the sheet.


The goal here, as the code demonstrates, is to evaluate the formulas mentioned, (the IF formula that works & the IF(AND formula ... that is not working) without having to loop through cells or an array.


Edit: Here is code for the EvaluateIF formula that works that can be run on the example data that I posted:
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 A range = 'Good' then change Column A
'                                                                                           '           cell to 'OK' Else leave the value as is
    End With
End Sub
 
Last edited:
Upvote 0
I thought what you were doing was pretty clear, after analyzing your formula and reading your comments. But I would tend to write a loop to do it in VBA rather than use Evaluate, just because I think it makes it more explicit in the code what you are trying to do. It doesn't mean there is anything wrong with your approach.

OTOH I have analyzed what you've done and I'm not sure why it doesn't work. I'm still trying to figure it out. I suspect it may be because your formula has to be an array formula--I do not know if Evaluate will automatically treat it as such. However, that does not explain why the first bit of code does work. It seems like they should both work, or both not work. Still troubleshooting.

EDIT: When I use your second formula in a cell, I get an array formula that gives correct results. So the logic is OK.
 
Last edited:
Upvote 0
To wit, your original data in A:B and array formulas in D1 and E1. However, the one in E1 does not give correct results. It merely returns the original data.

$scratch.xlsm
ABCDE
1DefinitelyGoodDefinitelyDefinitely
2DefinitelyGoodDefinitelyDefinitely
3MaybeBadMaybeMaybe
4GoodGoodOKGood
5MaybeBadMaybeMaybe
6DefinitelyGoodDefinitelyDefinitely
7DefinitelyBADDefinitelyDefinitely
8MaybeBADMaybeMaybe
9DefinitelyGoodDefinitelyDefinitely
10DefinitelyGoodDefinitelyDefinitely
Good Bad
Cell Formulas
RangeFormula
D1:D10D1=IF($A$1:$A$10= "Good", "OK",$A$1:$A$10)
E1:E10E1=IF(AND($A$1:$A$10= "Definitely", $B$1:$B$10= "Good"), "OK",$A$1:$A$10)
Dynamic array formulas.
 
Upvote 0
Try:

VBA Code:
With Range(Cells(1, 1), Cells(10, 1))
    .Value = Evaluate("IF((" & .Address & "= ""Definitely""" _
            & ")*(" & .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")
End With

(If you use AND, it will collapse the vector to a single value)
 
Upvote 0
Solution
Now that I see the comments way far to the right which I did not originally see.
It says this:
If cell in Column A range = 'Good' then change Column A
' ' cell to 'OK' Else leave the value as is

When I try it does not work.
 
Upvote 0
Try:

VBA Code:
With Range(Cells(1, 1), Cells(10, 1))
    .Value = Evaluate("IF((" & .Address & "= ""Definitely""" _
            & ")*(" & .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")
End With

(If you use AND, it will collapse the vector to a single value)
Thank you so much @StephenCrump! That works!
 
Upvote 0
Try:

VBA Code:
With Range(Cells(1, 1), Cells(10, 1))
    .Value = Evaluate("IF((" & .Address & "= ""Definitely""" _
            & ")*(" & .Offset(, 1).Address & "= ""Good""), ""OK""," & .Address & ")")
End With

(If you use AND, it will collapse the vector to a single value)
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.
 
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.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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