Solved

Quick VBA Question on Dim

Posted on 2009-04-02
8
299 Views
Last Modified: 2012-05-06
What are variables called that exist in VBA that are containers for items such as strings?

For example, Dim beh1 as string, Dim beh 2 as string
But let's say I have 20 behaviors and I have beh1 to beh20 and I would like to write a for loop that goes from beh1 to behN, where N is 20. How do I go about doing that?
0
Comment
Question by:allenlo77
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:Antagony1960
ID: 24050699
Are you thinking of arrays?

Dim beh(4) As String, i As Integer
    beh(0) = "a": beh(1) = "b": beh(2) = "c": beh(3) = "d": beh(4) = "e"
    For i = 0 to 4
        Debug.Print beh(i)
    Next
0
 
LVL 4

Expert Comment

by:Javin007
ID: 24050964
Maybe you're looking for a collection?

Dim Meh As Collection
Meh.AddItem(Beh1)

Then For/Each?
0
 

Author Comment

by:allenlo77
ID: 24051031
Both solutions would work for my question, but I am asking what is that beh1 called? For example if I wanted to combine beh and 1 to make beh1 that contains a string, how would I do that? What data type is beh? (The actual data type, and not what beh & "1" will contain, which is a string)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:allenlo77
ID: 24051103
Am I clear on what I am trying to accomplish and ask? Or is the method I am asking impossible? I have not dwell into variants much yet..
0
 
LVL 11

Expert Comment

by:Antagony1960
ID: 24051335
No, it seems that you want to refer to variables by a name derived from a string and I don't think that can be done in VB. Variant variables can contain almost any data type, but you still have to refer to it by its name as a part of your code.
0
 
LVL 11

Accepted Solution

by:
Antagony1960 earned 250 total points
ID: 24051709
Of course you could build a function which returns a variable's value when its name is passed as a string:

Private Function Getbeh(ByVal sVarName As String) As String
    Select Case sVarName
        Case "beh1": Getbeh = beh1
        Case "beh2": Getbeh = beh2
        Case "beh3": Getbeh = beh3
        Case "beh4": Getbeh = beh4
        Case "beh5": Getbeh = beh5
    End Select
End Function

'Usage example:

    For i = 1 To 5
        Debug.Print Getbeh("beh" & i)
    Next
0
 
LVL 4

Expert Comment

by:Xcone
ID: 24052261
I do not know how far you are willing to go. And I do not know if it is possible. But if it this, it would be through allowing access to your VBA projects from code.

Goto Extra -> Macro -> Security and check the box "Allow access to Visual Basic-project" (I use a dutch Windows, so name could be different).
When it is enabled, you will have access to your visual basic project using: ThisDocument.VBProject

Things like codecompletion are very limited when you go there, so you'll need some sort of reference. Also, I must press on the security matter. That box is there for a reason, so you create a mayor security leak by enabling it.
0
 
LVL 4

Expert Comment

by:Xcone
ID: 24052311
If access to VBA Project proves impossible or too much effort, and collections can't solve your desire, you could also try dictionaries.

I've never used them in VBA, but they should allow some more flexibility then Collections.
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_23195857.html
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question