Solved

Indirectly call a constant in vba

Posted on 2012-03-22
4
519 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

911 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

19 Experts available now in Live!

Get 1:1 Help Now