VBA Syntax uses only [ ]

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Quick VBA question.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
Syntax below work<o:p></o:p>
Method 1
Code:
With Range("L2:L" & LR)<o:p></o:p>
                .Formula = "=LEFT(M2,SEARCH(""_"",M2)-1)"<o:p></o:p>
                .Value = .Value<o:p></o:p>
End With<o:p></o:p>
<o:p> </o:p>
Tried changing it to <o:p></o:p>
<o:p> Method 2</o:p>
Code:
With [L2:L & LR]<o:p></o:p>
                .Formula = "=LEFT(M2,SEARCH(""_"",M2)-1)"<o:p></o:p>
                .Value = .Value<o:p></o:p>
End With<o:p></o:p>
<o:p> </o:p>
I get Error 2029.<o:p></o:p>
<o:p> </o:p>
I am trying to remove range and use [] to make code shorter.<o:p></o:p>

What code modification must I make to make Method 2 work with square brackets []?

Biz
 

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
I believe the info inside brackets is literal-- you can't use/build strings.

Check out documentation for worksheet.evaluate method.
 
Upvote 0
Hi Biz,

I agree with Greg. I do not believe you can supply a string argument to Evaluate using the shorthand notation (the brackets). In reading the help topic, I would suggest disregarding the part about the brackets making the code shorter. When I started using vba, I thought that looked neat too, but you will quickly see the downsides to using Evaluate just to refer to a Range.

Evaluate is not without its costs.

Each time it is called, it must first decide whether it is returning an object or a value. That slows things down.

Further - you lose intellisense.

Overall, IMO, while Evaluate can be really neat for occasional use, it is no panacea.
 
Upvote 0
Gregtx81 & Mark,

Thanks for your feedback. If understand right using [] does slow vba code?
Thanks I did not know dat.

Biz
 
Upvote 0
In addition to Gregs comments, which are correct, that you cannot use variables inside the square brackets.
While removing range will make the code shorter, it's not encouraged because of readability. Not a lot of people use the shorter version. So, if it's strictly for you only, go right ahead, but others may not follow the code.
You will also find that at some point you will have to use range for your code and then you have an issue with using different types in different places
Have a quick read here for a summary of it's use

Code:
http://support.microsoft.com/kb/104502
 
Upvote 0

Forum statistics

Threads
1,216,127
Messages
6,129,022
Members
449,481
Latest member
joaotcosta23

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