Solved

Quick VBA Question on Dim

Posted on 2009-04-02
8
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

740 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