Solved

Quick VBA Question on Dim

Posted on 2009-04-02
8
295 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now