I want to assign certain time weightage values to the variables of a data set. So I am trying to use Enumerations/Constants so to improve my code quality and make it easy to maintain. But on compiling my project, it throws in a 
CodePudding user response:
- The error message is incorrect: your code does not have any circular references.
- This is more of a bug in the VBA interpreter: your code is still incorrect and invalid, but VBA is showing the wrong error message.
- Given that VBA remains effectively frozen-in-time since 2002 (excepting 64-bit support in 2007), so don't expect any fixes, let alone any enhancements, ever (Though MS Office's COM automation tooling is slowly shifting to JavaScript (maybe Python? please?).
- The actual problem with your code is threefold:
- You cannot use a
Functionto initialize aConstvalue in VBA. - You cannot use a
Functionto define values for a VBAEnumeither. - You cannot have
Enummembers typed asDouble: VBA only supportsIntegerandLongvalues forEnummembers.- Curiously, VBA does allow
Constvalues to be typed asDouble- but most other languages don't becauseDoubleis an IEEE-754 floating point type that does not have a machine-portable representation, but as VBA is only for Win32 on x86/x64 I guess that means Microsoft made it work given the very narrow gamut of hardware that VBA programs will run on.
- Curiously, VBA does allow
- You cannot use a
Anyway, if you want "named values" typed as Double that you can use anywhere, then try this:
Create a new
Module(not aClass Module).Rename the Module from
Module1toWeightage.Put this code in
Weightage:Private Function ConvTohr(val As Integer) As Double ConvTohr = Round((val / 60), 2) End Function Public Property Get Var1_Weightage() As Double Var1_Weightage = ConvTohr(3) End Property Public Property Get Var2_Weightage() As Double Var2_Weightage = ConvTohr(11) End Property Public Property Get Var3_Weightage() As Double Var3_Weightage = ConvTohr(2) End Property Public Property Get Var4_Weightage() As Double Var4_Weightage = ConvTohr(9) End Property Public Property Get Var5_Weightage() As Double Var5_Weightage = ConvTohr(0) End PropertyScreenshot proof:
(See output in the Immediate pane):

