chr$

K

kylefoley2000

what does chr$ mean in this code

Sub rick()
Dim strabc(1 To 26) As String
Dim i As Integer
Dim strprompt As String
For i = 1 To 26
strabc(i) = Chr$(i + 64)
Next i
strprompt = "hey:" & vbCrLf
For i = 1 To 26
strprompt = strprompt & strabc(i)
Next i
MsgBox strprompt


End Sub
 
C

Chip Pearson

When a $ character is at the end of a string function such as Chr, it
tells VBA to use the String, as opposed to the Variant, version of
the function. In most respects, it is irrrelevant whether you use the
$ version of the function.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
 
B

Bob Phillips

Looking this up in VBA help we get

Returns a String containing the character associated with the specified
character code.
 
J

Joe User

kylefoley2000 said:
what does chr$ mean in this code

It's superfluous.

Putting "$" after a variable name ensures that it is treated as String
variable, even if it is not declared as such (and it is not declared as
something else, and it Option Explicit is not declared).

But putting "$" after a function name has not functional value since
functions, especially intrinsic VBA functions, are typed explicitly.

However, some people might argue that putting "$" after any name is
self-documenting. That is, it makes it clearer to the reader what the code
is doing.

There are many other date-type suffixes. "%" for Integer; "#" for Double;
and "@" for Currency, to name a view. These numeric suffixes are especially
useful following constants.


----- original message -----
 
J

Joe User

Chip Pearson said:
When a $ character is at the end of a string function
such as Chr, it tells VBA to use the String, as
opposed to the Variant, version of the function.

I did not know there were two versions and they can co-exist. The Chr
Function Help page describes only a function that returns type String. Live
and learn!

I presume the ability to have two versions of functions(different types) is
limited to intrinsic VBA functions. I get an error when I try to create a
Variant and String function with the same name in the same module. When I
create Public Variant and String functions with the same name in different
module, Call funcName$ calls whichever function is in the same module, even
it is Variant function(!); but x = funcName$ raises a compiler error when
called in the module with the Variant function declaration. Call funcName$
also raises a compiler error when called from a module with no function
declaration of the name.


----- original message -----
 
J

Joe User

kylefoley2000 said:
great answer

Maybe not. Stay tuned for Chip's response. It was posted to another
server. It takes 30-40 min to propagate to the MSDG web server, in my
experience.

(It should be showing up momentarily.)


----- original message -----
 
R

Rick Rothstein

Joe gave you a great answer to a question you did not ask. Joe's answer
dealt with variable names and data type suffix characters, however you asked
about chr$ which is not a variable. Chr is a built-in VB String function and
most (but not all) String function have two forms... one that returns String
value directly (those have the $ sign suffix attached to them) and another
which returns a Variant value having a sub-type of String. In theory, using
the String value version (with the $ sign) is slightly faster than using the
Variant value version. The time difference is pretty much not noticeable
unless you have a huge loop performing extensive String manipulations
(making use of those functions) during each loop, and even then the time
differences should be somewhat smallish.
 
J

JLatham

And to add yet more confusion to the whole thing:
In the Beginning there was BASIC and in BASIC there was CHR$(), but there
was no CHR(). Likewise there was DIR$() and it was without DIR(). And there
are similar examples of the original BASIC language that had the $ as a
required part of the function name that have operators now that have dropped
the $ and yet act in exactly the same manner, and are generally
interchangeable.
 
J

JLatham

Oh - and I believe Bob Phillips actually answered the question: it returns a
character based on the numeric value derived by adding the 64 to the value of
i.
With i=1 to 26, you'll end up returning characters A through Z.
 
J

JLatham

Naaaahhhh...
Boring would have been telling him that the second loop is going to output
hey:
A
followed by
hey:
B
followed by
hey:
C
ad nauseum, or until sleep overcomes one of us!
<g>
 
J

Joe User

JLatham said:
In the Beginning there was BASIC and in BASIC there was CHR$(),
but there was no CHR(). Likewise there was DIR$() and it was without
DIR().

"In the beginning", BASIC had no type declarations. Instead, the type was
implied by naming conventions. Original BASIC had only string and numeric
variables. Some versions of BASIC evolved additional naming conventions,
such as suffixes to distinguish integer and floating-point variables. All
of this predates Microsoft and MS BASIC by about two decades, and it
predates the ANSI BASIC standard by more than a decade.

So what? That is really a very different language than MS Visual BASIC and
VBA.


----- original message -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top