?
Solved

Quick VBA Question on Dim

Posted on 2009-04-02
8
Medium Priority
?
305 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 1000 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

Technology Partners: 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!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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