What does a colon in VBA code do? I.e.,
Hide:
I tried to figure out how to look this up both online and in
Walkenbach, but couldn't find anything on it.
Thanks.
What does a colon in VBA code do? I.e.,
Hide:
I tried to figure out how to look this up both online and in
Walkenbach, but couldn't find anything on it.
Thanks.
It separates two logical lines of code into a single line of
text. E.g., the following code snippets are functionally
equivalent:
Dim X As Long: X = 123
Dim X As Long
X = 123
"davegb" <[email protected]> wrote in message
news:[email protected]...
> What does a colon in VBA code do? I.e.,
>
> Hide:
>
> I tried to figure out how to look this up both online and in
> Walkenbach, but couldn't find anything on it.
> Thanks.
>
I should have added that the colon is also used to label a
location in code, typically used for On Error Goto Label
statements.
On Error GoTo ErrH:
' some code
ErrH:
' error code here
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
"davegb" <[email protected]> wrote in message
news:[email protected]...
> What does a colon in VBA code do? I.e.,
>
> Hide:
>
> I tried to figure out how to look this up both online and in
> Walkenbach, but couldn't find anything on it.
> Thanks.
>
A c olumn ":" represents a label for a certain part of code, for example:
Sub Example()
Dim myValue As Boolean
myValue = False
If myValue = True Then
GoTo Section1 'the execution will continue starting from Section1:
Else
GoTo Section2 'the execution will continue starting from Section2:
End If
Section1: 'label
MsgBox "Section1"
Exit Sub 'this is necessary to stop here the execution instead of go on with
Section2, too
Section2: 'label
MsgBox "Section2"
End Sub
This example only shows which is the fuction of column ":", it doesn't do
anything useful.
Please, note that GoTo sections are not a very good choise in almost all
cases: the code will result more diffucult to be followed, more exposed to
bugs etc... So, use GoTo only if strictly necessary, as I was always told,
too.
Hoping this is clear.
Ciao,
Lorenzo
--
******************************************
myWeb: http://www.lorenzospace.it
******************************************
"davegb" <[email protected]> ha scritto nel messaggio
news:[email protected]...
> What does a colon in VBA code do? I.e.,
>
> Hide:
>
> I tried to figure out how to look this up both online and in
> Walkenbach, but couldn't find anything on it.
> Thanks.
>
In this case, I suspect "Hide:" is a line label (Colons can also be used to
separate multi commands on a single line, but that doesn't seem to be the
case here). There is probably a "GoTo Hide" instruction somewhere in the
same function/procedure where you found it (if there isn't, there probably
was one at some point in time and someone simply forgot to take the
corresponding label out.)
Use of line labels and goto's is (generally) frowned upon (outside of Error
trapping where they are a necessity). This has become one of those "rules to
be broken" because improper/overuse of them is an very easy trap for newbie
coders to fall into since there is (almost) always a better way to achieve
the same results. Extreme caution is advised or you could quickly end up
with "spaghetti code" that jumps all over the place: something totally
unreadable & unmanageable, even if you wrote it yourself yesterday.
"Line labels" doesn't warrant an entry in Help, but you can get to a
hyperlink definition of them via the entry for the "GoTo" statement.
Let's be careful out there...
HTH,
--
George Nicholson
Remove 'Junk' from return address.
"davegb" <[email protected]> wrote in message
news:[email protected]...
> What does a colon in VBA code do? I.e.,
>
> Hide:
>
> I tried to figure out how to look this up both online and in
> Walkenbach, but couldn't find anything on it.
> Thanks.
>
George Nicholson wrote:
> In this case, I suspect "Hide:" is a line label (Colons can also be used to
> separate multi commands on a single line, but that doesn't seem to be the
> case here). There is probably a "GoTo Hide" instruction somewhere in the
> same function/procedure where you found it (if there isn't, there probably
> was one at some point in time and someone simply forgot to take the
> corresponding label out.)
>
> Use of line labels and goto's is (generally) frowned upon (outside of Error
> trapping where they are a necessity). This has become one of those "rules to
> be broken" because improper/overuse of them is an very easy trap for newbie
> coders to fall into since there is (almost) always a better way to achieve
> the same results. Extreme caution is advised or you could quickly end up
> with "spaghetti code" that jumps all over the place: something totally
> unreadable & unmanageable, even if you wrote it yourself yesterday.
>
> "Line labels" doesn't warrant an entry in Help, but you can get to a
> hyperlink definition of them via the entry for the "GoTo" statement.
>
> Let's be careful out there...
>
> HTH,
> --
> George Nicholson
>
> Remove 'Junk' from return address.
>
>
> "davegb" <[email protected]> wrote in message
> news:[email protected]...
> > What does a colon in VBA code do? I.e.,
> >
> > Hide:
> >
> > I tried to figure out how to look this up both online and in
> > Walkenbach, but couldn't find anything on it.
> > Thanks.
> >
Thanks for all your replys!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks