Solved

Indirectly call a constant in vba

Posted on 2012-03-22
4
518 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now