Solved

Quick VBA Question on Dim

Posted on 2009-04-02
8
298 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

803 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