Solved

Indirectly call a constant in vba

Posted on 2012-03-22
4
523 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
4 Comments
 
LVL 33

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

809 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