Evaluate in VBA & If({1},

mse330

Well-known Member
Joined
Oct 18, 2007
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I am starting to use Evaluate in my VBA codes but looks like since I have office 365 & dynamic arrays, it does something for me that makes it work but it doesn't work for older office versions. I have seen some members here use If({1}, to overcome this issue I guess so is this a guaranteed method that should work in all cases ? Shall I just always use it to ensure that my code doesn't fail if used in older excel versions ? What does If({1}, means/signifies ?

I would appreciate your feedback & in case you have a good link to share, please do so

I have put the below example for illustration which makes not difference in my machine since I am using office 365

VBA Code:
Sub test()

With Range("A2:A11")
  .Offset(, 1).Value = Evaluate("len(" & .Address & ")")
  .Offset(, 2).Value = Evaluate("if({1},len(" & .Address & "))")
End With

End Sub

Book1
ABC
1Statewithout If({1},with If({1},
2Colorado88
3New York88
4California1010
5Washington1010
6Texas55
7Ohio44
8Arizona77
9New Jersey1010
10Pennsylvania1212
11North Dakota1212
Sheet1
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Shall I just always use it to ensure that my code doesn't fail if used in older excel versions ?
Really depends on what formulae you use, but for your example, I would say Yes.
The IF forces the formula to look at a specific row, rather then the complete range & the {1} effectively returns TRUE, so you dont need a false part to the IF.
 
Upvote 0
Thanks Fluff for your reply. So what does the curly bracket means here ? Does it convert the formula to array formula ? If the intention is to have a True then would If(1=1, work ?
 
Upvote 0
My understanding (probably "guess" is a better word) of it is this. Some functions in Excel (notably text functions) are not natively array-aware, however, if included within a function that is array-aware, that function induces (not sure of the actual word that would apply) the non-array-aware function to become array-aware. The IF function is the array-aware function that does that. The {1} is just an array that is given to the IF function to force it to use its array processing functionality. You would use {1} only if the range of cells being processed has no blanks (otherwise the blanks would become zeros in the output). If you have blanks, the IF structure you should use is IF(range="""","""",formula) instead of IF({1},formula).
 
Upvote 0
IF(1,A1:A6) will return a reference of A1:A6, IF({1},A1:A6) will return an array contains the values of A1:A6 which is ={"A";1;"B";2;"C";3}.
See below example, use N function for IF(1,A1:A6) and IF({1},A1:A6) will return different result. N(reference) will get only the top left cell A1 and change it to 0, N(value array) will change each text item of the array to 0.
工作簿3
ABCD
1A00
211
3B0
422
5C0
633
Sheet1
Cell Formulas
RangeFormula
C1:C6C1=N(IF({1},A1:A6))
D1D1=N(IF(1,A1:A6))
Dynamic array formulas.
 

Attachments

  • 1592663075889.png
    1592663075889.png
    63.3 KB · Views: 4
Upvote 0
Thanks @Rick Rothstein for the explanation. You are right, I just deleted one of the cells then I got 0 ... I tired the below code & works as expected but would it work correctly in older version of office without the If({1} ?

VBA Code:
Sub test()

With Range("A2:A11")
  .Offset(, 1).Value = Evaluate("len(" & .Address & ")")
  .Offset(, 2).Value = Evaluate("if({1},len(" & .Address & "))")
  .Offset(, 3).Value = Evaluate("if(" & .Address & "="""","""",len(" & .Address & "))")
End With

End Sub
 
Upvote 0
Thank you @shaowu459 ... I have tried your formulas & I think it makes more sense now. I just need to get my hands on older excel version to try out different scenarios for better understanding. With office 365, it creates a dynamic array once I pasted your formula in cell C1 as shown below whereas it doesn't in cell D1

Thank you all for your time to explain :)

Dynaimc Formula.PNG
 
Upvote 0
I tired the below code & works as expected but would it work correctly in older version of office without the If({1} ?

.Offset(, 3).Value = Evaluate("if(" & .Address & "="""","""",len(" & .Address & "))")

In case anyone finds this thread later on, I just tried the option suggested by Rick & it works as expected in Excel 2007 without the If({1},
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

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