Advice reqd on Constants

smiler44

Registered User.
Local time
Today, 10:59
Joined
Jul 15, 2008
Messages
678
I have a workbook with a few modules in. Two of the modules contain nearly identical macros and constants. The names of the constants are the same although the values may differ.
Can the constants in one module have any effect on the macro in the second module even if the values to the constants are set differently in the second module?

I know it would be better to change the name of the constants but for the moment copy and paste was quick. If the naming is the problem then it’s easy to deal with. If it’s something else causing my problem then mmmm not sure what to do except keep trying.

Constants shown below

Smiler44


Code:
 Option Explicit 'force variables to be declared before use
        
   Dim srcTotalsColOffset As String
              
   '*** useful constants describing where the source data comes from
   
   Const SourceDataSheet = "sheet1"
   Const SrcRowOffset = 3  'How many Rows after the product name that the data starts
   Const SrcOUCColOffset = 2  'How many columns across from the product name that the product data starts
   Const OUCSigChars = 3  'How many characters for a match when comparing codes?
   '*** useful constants describing where the output data goes
   Const OutputDataSheet = "sheet2"
   Const OutputDataOUCRow = 13     'Where we find the list of codes (running across the row)
   Const OutputDataTotalRow = OutputDataOUCRow + 2    'Where we write the total for the code
   Const OutputDataStartCol = 2    'First column
 
Hi, smiler44,

the "magic" word is scope: if a variable is dimensioned in a module it is only known in that module. If you dimension a variable with Dim in the head of a module it will only be available to the procedures in that module (except they dimension a local version of the same name). This could be notified by using the prefix m. Dimensioning a variable as Public will make that variable available to all procedures and codes in the workbook (unless... already mentioned). This could be marked by using the prefix g.

Depending on the complexity of a workbook it might be a good idea to set everything up using contants and alter the values afterwards in one place if any changes in the layout still have to be made.

HTH
Holger
 
I would read help on constants paying attention to Public/Private and see what applies in your case.

Brian
 
If I have understood Holger correctly it should not matter that I have used the same name in different modules. Thank you.

Since posing the question I created a new seperate workbook to test the code I was using. Somehow and I have yet to print the code and read through it comparing it to what was in the orginal workbook I seem to have accidently solved the problem. What ever I have done was not a deliberate attempt to get the code to work, as I was trying to restore it to it's original state and start again. It seems to good to be true and so I will test it a number of times before being satisfied it works.

A bad week ends well

smiler44
 

Users who are viewing this thread

Back
Top Bottom