Suppose we’ve some text values in range A1:A10 and we want to extract and keep only the first three letters of the values in all cells of this range. We could try to do so using the below code:
Code:
Set rngData = ThisWorkbook.Worksheets("Sheet1").Range("A1:B10")
rngData = Evaluate("Left(" & rngData.Address & ",3)")
BUT, you’ll find it does not work. It will fill the whole range with first 3 letters of cell A1. The reason is that
if the Excel function used in Evaluate does not accept an array, the
Evaluate function will not return an array. So in order to make this function return an array we need to modify the code slightly like this:
Code:
Set rngData = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
rngData = Evaluate("if(Row(1:10),left(" & rngData.Address & ",3))")
In this case,
ROW(1:10) returns an array of numbers from 1 to 10. Any numeric value other than 0 returned from a logical function is considered as TRUE, So there are 10 vertical TRUE values. For each TRUE, it will return the corresponding cell’s value from A1:A10.
Bookmarks