Solved

Indirectly call a constant in vba

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

735 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