Max length of variable name?

D

David

Hi,

Sorry if this appears twice, could not tell if first posted.

What is the maximun length of a varaible name?

x=1 not problem, but
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=2 this is too long, yes? How long can it be?

Thanks,
 
R

Ron Rosenfeld

Hi,

Sorry if this appears twice, could not tell if first posted.

What is the maximun length of a varaible name?

x=1 not problem, but
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=2 this is too long, yes? How long can it be?

Thanks,

You did not write what type of variable you are using, or why you think
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=2 is "too long". That makes it difficult to
advise you.

Since this is the programming group, and most posting about variable names in
this group would be using VBA, then:

"Variable names must begin with an alphabetic character, must be unique within
the same scope, can't be longer than 255 characters, and can't contain an
embedded period or type-declaration character."

Since this works:

=======================
Sub foo()
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 2
Debug.Print xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End Sub
======================

you are going to have to be more detailed in describing your problem.

--ron
 
J

JLGWhiz

If you are passing the variable to a procedure, you are limited to 255
readable characters. The rest will be truncated. Outside of that, I have
not found a restriction on the number of characters. I suppose the correct
answer is that if it exceeds the length of the receptacle it will be used
in, then all excess characters will be truncated and unreadable.
 
R

Rick Rothstein

According to the "Visual Basic Naming Rules" section in Excel's VBA Help
Files, the maximum length of a variable name (also procedures, constants,
and arguments) is 255 characters; but, quite frankly, for practicality
reasons, your names should not be getting anywhere near that long.
 
D

David

Hi,

The variables contain numbers.

The problem I had:
Tolerance = GrossTotal * 0.01 (This came out to about 1000)
TargetMMktPercent = ActiveCell.Offset(56, 0).Value
TargetMMkt = TargetMMktPercent * GrossTotal
TargetMMktHi = TargetMMkt + Tolerance
TargetMMktLo = TargetMMkt - Tolerance
(The last 2 statements had the same values and I had to change then to:
TargetMHi = TargetMMkt + Tolerance
TargetMLo = TargetMMkt - Tolerance
to get different values)

Thus the question about the length of a variable name, which if allowed 256
characters does not seem to make sense.

This did work:
Sub foo()
x = 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 2
Debug.Print x & " " & xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End Sub

Thanks
 
D

David

Hi,

I am at a loss to explain why the above gave me a problem, but I guess since
I changed it, it no longer matters.

Thanks for your help.
 
J

JLGWhiz

The variable is limited. The value of the variable is subject to the
conventions of the application. i.e. Excel will automatically convert some
numbers to scientific format which would be less characters than the actua
value contained in VBA. Also Excel will round certain numbers, which again
would be different than the actual value defined by VBA. So, if you are
attempting to draw some static conclusion as to the Variable vs Variable
value, you will find it difficult to do without an understanding of how
Excel interacts with VBA.
 
R

Ron Rosenfeld

Hi,

The variables contain numbers.

The problem I had:
Tolerance = GrossTotal * 0.01 (This came out to about 1000)
TargetMMktPercent = ActiveCell.Offset(56, 0).Value
TargetMMkt = TargetMMktPercent * GrossTotal
TargetMMktHi = TargetMMkt + Tolerance
TargetMMktLo = TargetMMkt - Tolerance
(The last 2 statements had the same values and I had to change then to:
TargetMHi = TargetMMkt + Tolerance
TargetMLo = TargetMMkt - Tolerance
to get different values)

Thus the question about the length of a variable name, which if allowed 256
characters does not seem to make sense.

This did work:
Sub foo()
x = 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 2
Debug.Print x & " " & xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End Sub

Thanks
--

1. You still haven't provided any information I can see as to why you think a
variable name is too long. Are you getting an error message? Unexpected
result? System crash? etc. If so, what is the nature of the error message
(what does it say) or other dysfunction.

2. How are your variables DIM'd?
3. Where do the values for these variables come from -- How did the numbers
get into them?

We really can't read minds, so the more information you can provide, the easier
it is to help you.
--ron
 

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