Solved

Indirectly call a constant in vba

Posted on 2012-03-22
4
527 Views
Last Modified: 2012-03-22
Hi
I want to tidy up this code
There are many Different Products and each of the 800 odd shops will have it's own discounts for each of the products

  
            Select Case LCase(sProduct)
            Case "R"
                Select Case Range("SalesPersonCurrentStore").Value
                Case gcstShopName001
                    TempDisc = gcstDiscountSpecialR_Top001
                Case gcstShopName002
                    TempDisc = gcstDiscountSpecialR_Top002
                Case gcstShopName003
                    TempDisc = gcstDiscountSpecialR_Top003
                Case gcstShopName004
                    TempDisc = gcstDiscountSpecialR_Top004
                Case gcstShopName005
                    TempDisc = gcstDiscountSpecialR_Top005
                Case gcstShopName006
                    TempDisc = gcstDiscountSpecialR_Top006
                Case gcstShopName007
                    TempDisc = gcstDiscountSpecialR_Top007
                Case Else
                    MsgBox "Unknown Shop" & vbCrLf & "No discount"
                    TempDisc = 0
                End Select
            Case "e"
                Select Case Range("SalesPersonCurrentStore").Value
                Case gcstShopName001
                    TempDisc = gcstDiscountSpecialE_Top001
                Case gcstShopName002
                    TempDisc = gcstDiscountSpecialE_Top002
                Case gcstShopName003
                    TempDisc = gcstDiscountSpecialE_Top003
                Case gcstShopName004
                    TempDisc = gcstDiscountSpecialE_Top004
                Case gcstShopName005
                    TempDisc = gcstDiscountSpecialE_Top005
                Case gcstShopName006
                    TempDisc = gcstDiscountSpecialE_Top006
                Case gcstShopName007
                    TempDisc = gcstDiscountSpecialE_Top007
                Case Else
                    MsgBox "Unknown Shop" & vbCrLf & "No discount"
                    TempDisc = 0
                End Select
            Case "g"
                Select Case Range("SalesPersonCurrentStore").Value
                Case gcstShopName001
                    TempDisc = gcstDiscountSpecialG_Top001
                Case gcstShopName002
                    TempDisc = gcstDiscountSpecialG_Top002
                Case gcstShopName003
                    TempDisc = gcstDiscountSpecialG_Top003
                Case gcstShopName004
                    TempDisc = gcstDiscountSpecialG_Top004
                Case gcstShopName005
                    TempDisc = gcstDiscountSpecialG_Top005
                Case gcstShopName006
                    TempDisc = gcstDiscountSpecialG_Top006
                Case gcstShopName007
                    TempDisc = gcstDiscountSpecialG_Top007
                Case Else
                    MsgBox "Unknown Shop" & vbCrLf & "No discount"
                    TempDisc = 0
                End Select
            Case Else
                    MsgBox "Unknown Product" & vbCrLf & "No discount"
                    TempDisc = 0
                Exit Sub
            End Select
.....

Open in new window


I would like to use something like the indirect spreadsheet function but to call a constant

something like
TempDisc =  _
IndirectConstantCall("gcstDiscountSpecial" & gstrProductCode & "G_Top" & gstrShopName)

I know this doesn't exist and the only other way I can think of doing it is with a multi dimensional array.

I do not want to use any worksheet or worksheet functions as I am porting it out of excel into vb soon.

Any suggestions as to the right way to do this and how to go about it for the above example
0
Comment
Question by:sirplus
[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
4 Comments
 
LVL 34

Assisted Solution

by:Norie
Norie earned 100 total points
ID: 37752660
Why not use arrays for the discounts and shop names?

Dim gcstDiscountSpecialG_Top(1 To 100, 1 To 5)
Dim gcstShopName(1 To 100)

Open in new window

The discounts array has 2 dimensions so you can store discount R, discount R etc.

You could even use the first dimension for the store and you would just have one array with all the data.
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 100 total points
ID: 37752664
I'd use an array and I'd probably load the array data from a file if need be. That way you can simply alter the file rather than change the code if the discount changes. (particularly useful if you are going to VB proper!)
0
 
LVL 3

Accepted Solution

by:
DaFranker earned 300 total points
ID: 37752702
My first suggestion would be to use a Collection object. It adds a bit of memory overhead, but you can use what you currently use as the constant's name as the collection key, with the constant's value as the variable.

Then, calling by key allows you to make the call pretty much the same way you lined up those strings in that imaginary IndirectConstantCall() function above.

Ideally, for ease of modification later on, you'd also externalize your "constants" into a database or some kind of external file listing the keys and values you want, and then iterate through the DB table or parse the file to populate the Collection.

I'd strongly recommend against putting all your discount values as constants in some obscure constant declaration somewhere directly in the code.

Also, using generic names and arrays/collections allows you to use loops and much cleaner code, rather than giant Select Case blocks.

Addendum: The reason I suggest a Collection first before array(s) is that with a Collection Object you don't need to worry about keeping track of your array dimensions and size, you just .Add elements as needed and call them by name (key) in whatever way you want. Arrays require knowing in advance how many values you'll have or using Redim Preserve statements in loops (with careful code revision and testing to make sure you don't miss a beat).
0
 
LVL 5

Author Comment

by:sirplus
ID: 37753511
i will use something based on this & load it from a file
 
Sub Test_Enumerations()

Dim colMyCollection As New Collection
Dim colSubCollection As Collection
For i = 1 To 10
Set colSubCollection = New Collection
colSubCollection.Add Item:=i + 6, _
Key:="MySixPlusVal"
colSubCollection.Add Item:=i + 3, _
Key:="MyThreePlusVal"
colMyCollection.Add Item:=colSubCollection, _
Key:=CStr(i)
Set colSubCollection = Nothing
Next i
For Each colSubCollection In colMyCollection
MsgBox colSubCollection.Item("MySixPlusVal")
Next
End Sub

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 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