Peculiar syntax in VBA for Excel, concerning ranges

corentint

New Member
Joined
Jan 31, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I have acquired a chess Excel Worksheet that works fine. However I was interested in the VBA programming behind the worsksheets (I am proficient, yet not expert in VBA Excel).

I came accross this peculiar syntax, which works:

[FEN_New] = sFENiniEN

Where FEN_NEW is actually a named range in the Excell spreadsheet
And sFENiniEN is actually a "Public Const sFENiniEN As String" in other words a VBA value only

I modified [FEN_New] to Range("FEN_New") and the code run without a hitch, of course.

Then I went to one of my own program in another completely independant workbook, and replace a few Range("namedrange") with this syntax [Namedrange], and ran the code, without any problems!
First time I see a usage of [ and of ].

Now which is easier to code, particularly if you use only named range in your worksheets:

[FEN_New] = sFENiniEN
Range("FEN_New")= sFENiniEN

?

I also tested the reverse (xFENiniEN = [FEN_New].value), works fine (used xFENiniEN instead as a variable, xFENiniEN being a constant, value cannot be changed).

My simple questions:
  • Is this shortcut syntax well known? (I searched high and low on the web, nothing found; I also have books about VBA for Excel, never saw this; I do know about @ # $ % to dim variables)
  • If it is known, is it advisable to use it? In other words, no short comings?
Quite interested in a reply. Thank you.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi - Interesting question!

I've known about it for as long as I can remember, and I assumed everyone else also knew about it, but I guess not! You may interested to learn (if you weren't already aware) that it isn't limited to named ranges, so:
VBA Code:
[A1:D1] = Array("Testing",1,2,3)
will work.

As for the $, % and so on, I was aware of that from my other BASIC days (which is also legacy VB, I think) - Dim FirstName$ and so on. In the same vein, do you know about DefStr, DefInt, DefLng, etc? :-)

As for whether it is advisable or not, I don't really have a view. I've not encountered any problems with using it, save that I tend to only really use it on cheap-n-nasty code - so, when I want to code up something quickly to test something, for example. I'd be interested in what others might have to say.
 
Upvote 0
Solution
Thank you. I was curious about this, but I guess for me it also means easier coding, especially since it works also for non named ranges.
Yes, the $, % and so on I knew from my BASIC coding days, but according to Walkenbach they were also in the original VB syntax. Coders appears to frown on their usage ... don't know why.
And no, I do not know either about DefStr, DefInt, DefLng, etc. Can you expand briefly? I assume Define as String, define as integer... but one of my teacher told me never to assume!
 
Upvote 0
It is a way of setting the default data type for variables at the modular level. So:
VBA Code:
DefStr A-C
Will set the default data type to string for all variables in that module that begin with A, B, or C. Again this is legacy stuff I'm told, and also the sort of thing that coders appear to frown on.

Here is the official documentation on DefTypes: Deftype statements (VBA)
 
Upvote 0
Oh, pretty specific and, probably, rare in usage. OK. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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