VBA Names madness

J

Jag Man

Every time I think I've got the concepts and syntax for Names in VBA
straight I discover
something I don't know how to do. The current problem is exemplified below:

Private Sub TheButton_Click()
Dim nIn As Integer, nOut As Integer

nIn = [mixer1!nInputs] ' This works... nIn is 6

Dim str As String
str = ActiveSheet.name & "nInputs" ' this forms "mixer1!nInputs"
nIn = [str] ' Fails, obviously because str is a string.

MsgBox (nIn)

End Sub

nInputs has been defined as a Name local to Worksheet mixer1, and the cell
which it RefersTo has the
integer 6 in it. Thus [mixer1!nInputs] gives, as it should, 6. However, I
want to generalize this
so as to pick up the nInputs value from the ActiveSheet, as opposed to
hard-wiring a particular
sheet name.

The (obviously lame) attempt builds "mixer1!nInputs", but since it is a
String there is a type mismatch for
the Evaluation operator [expression]. Now, I can understand that, as I do
understand the difference
between xxx and "xxx". What I can't come up with is the proper syntax for
what I want to do.

Help!

TIA

Ed
 
A

Allan Koodray

I think the context you are looking for is:

"mixer1!" & nInputs

Allan Koodray
 
D

Dave Peterson

the square brackets are short for evaluate.

This seemed to work ok for me:

str = ActiveSheet.Name & "!nInputs" ' this forms "mixer1!nInputs"
nIn = Application.Evaluate(str)
MsgBox nIn





Jag said:
Every time I think I've got the concepts and syntax for Names in VBA
straight I discover
something I don't know how to do. The current problem is exemplified below:

Private Sub TheButton_Click()
Dim nIn As Integer, nOut As Integer

nIn = [mixer1!nInputs] ' This works... nIn is 6

Dim str As String
str = ActiveSheet.name & "nInputs" ' this forms "mixer1!nInputs"
nIn = [str] ' Fails, obviously because str is a string.

MsgBox (nIn)

End Sub

nInputs has been defined as a Name local to Worksheet mixer1, and the cell
which it RefersTo has the
integer 6 in it. Thus [mixer1!nInputs] gives, as it should, 6. However, I
want to generalize this
so as to pick up the nInputs value from the ActiveSheet, as opposed to
hard-wiring a particular
sheet name.

The (obviously lame) attempt builds "mixer1!nInputs", but since it is a
String there is a type mismatch for
the Evaluation operator [expression]. Now, I can understand that, as I do
understand the difference
between xxx and "xxx". What I can't come up with is the proper syntax for
what I want to do.

Help!

TIA

Ed
 

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