VBA - Evaluate throwing error when [] form used

Johnny C

Well-known Member
Joined
Nov 7, 2006
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
I've been idling a dull afternoon away tidying up some VBA that works on ranges using the Evaluate function. More out of curiosity than anything else. I'm keen to understand it, I once came across a piece of horrible spaghetti code that used [Sum(Indirect(formula))] and I just gave up at that point and rewrote it but it would be nice to be able to decipher it.

This works, it trims a whole range of text without a loop:
Code:
Sub TrimData()
Set Rng = Selection
Rng.Value = Evaluate("if(row(),Trim(" & Rng.Address & "))")
End Sub

but when I tried to use the [] form it returns a #NAME ! error

Code:
Sub TrimData2()
Set Rng = Selection
Rng.Value = ["if(row(),Trim(" & Rng.Address & "))"]
End Sub

Debug.Print shows it's giving an error 2029, the same if I remove the quotes
Rng.Value = [if(row(),Trim(Rng.Address))]

I'm guessing I've not got the hang of the syntax of the [] form, what should I put?
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I dont believe you can use the square brackets with the variable. This for instance works fine:

Code:
Range("A1:A10") = [IF(ROW(),TRIM(A1:A10))]
 
Upvote 0
I dont believe you can use the square brackets with the variable.
That is correct... the argument to the square brackets is not a text string, so you cannot concatenate variables into it. As a matter of fact, you cannot put anything that requires VBA to evaluate it or parts of it between the square brackets. The square brackets give you a method to evaluate an Excel formula (with some limitations though) within the VBA world and have the evaluated value or array returned to the running code.
 
Upvote 0
That would explain why the original
Code:
[Sum(Indirect([I]formula[/I]))]
threw an error, I guess the author didn't know that and was unable to stop it erroring.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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