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
 
I don't think LEN is considered a "text" function because it returns a numerical result and Excel can handle numerical arrays... I was thinking more of functions like MID, LEFT, and so on as ones that require the array "inducement" (as I have termed it). Usually when I am unsure, I try it first without the array "inducement" and if that does not work, then I add it.
 
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)
I have tested the below code in my old laptop with Excel 2007 and it only gives me 8 for all rows based on my sample in post #1 which is the length of the word "Colorado" ... The reason I asked this question is in Excel 365 it will work either way so that's why I wanted to understand the logic as I can't refer back to older versions all the time to test :)

VBA Code:
Sub test()

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

End Sub
 
Upvote 0
It appears the LEN function must be induced. This work correctly (assuming no blank cells in the range)...
VBA Code:
.Offset(, 1) = Evaluate("IF({1},LEN(" & .Address & "))")
 
Upvote 0
Yes Rick, the one with the If({1} works as expected in Excel 2007. I guess the question is which function/cases that must be "induced" to give the correct results as a rule of thumb. I don't know if this is documented anywhere ...
 
Upvote 0
As far as I know, it is not documented anywhere. Almost all of what I "know" (I am using that term quite loosely) about this was derived from dogged trial and error. As I said earlier, I attempt to do the Evaluate without the "inducement" and if I don't get the answer I am expecting, I then try it with the "inducement".
 
Upvote 0
Ok, so I had some time today & I was experimenting in my old laptop with Excel 2007 and I have come up with the below conclusions (please comment if anything is incorrect) ... Hope this thread will be beneficial for others in the future

  1. I have used most of the common non-array formulas & all of them will not work without converting the Evaluate to an array formula, hence using If({1} or other method
  2. Direct simple calculations perform fine as expected without the need to convert the Evaluate to an array
  3. Some of the functions will fail & not work using Evaluate in a range. The same functions will show a #VALUE! error even if used in Excel 365 sheet & will not make a dynamic array like other functions (cells of those functions highlighted in yellow below). If anyone knows why please enlighten us

Cell Formulas
RangeFormula
C2C2=ABS(B2)
D2:D25D2=FORMULATEXT(C2)
C3C3=CHAR(B3)
C4C4=DAY(B4)
C5C5=EDATE(B5,2)
C6C6=EOMONTH(B6,2)
C7C7=EVEN(B7)
C8C8=EXACT(B8,A8)
C9C9=FIND("e",B9,1)
C10C10=HOUR(B10)
C11C11=ISEVEN(B11)
C12C12=ISBLANK(B12)
C13C13=ISTEXT(B13)
C14C14=LEN(B14)
C15C15=LEFT(B15,1)
C16C16=LOWER(B16)
C17C17=MID(B17,1,1)
C18C18=MROUND(B18,10)
C19C19=NETWORKDAYS(A19,B19)
C20C20=PROPER(B20)
C21C21=SEARCH("E",B21,1)
C22C22=TEXT(B22,"##%")
C23C23=TRIM(B23)
C24C24=WEEKDAY(B24)
C25C25=WEEKNUM(B25)
 
Upvote 0

Forum statistics

Threads
1,216,474
Messages
6,130,841
Members
449,598
Latest member
sunny_ksy

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