Automatically change the sign of variables

abberyfarm

Well-known Member
Joined
Aug 14, 2011
Messages
733
Hello,

I have two macros Mode5() and Mode6(). Each macro has 3 variables a1, a2 and s.

Code:
Sub Mode5()
    
   
    Const a1 As Single = 1
    Const a2 As Single = 2   
    Const s As Single = 3    
.........
End sub

Sub Mode6()
    
   
    Const a1 As Single = -1
    Const a2 As Single = -2   
    Const s As Single = -3    
.........

However, I'm will have trouble in subsequent macros where I call on the variables to be used in calculations because I have them declared twice.

The values for these variables will always be the same in both mode5() and mode6() and only the signs will different. Mode5() always positive and mode6() always negative. Therefore I was hoping that I could just declare the values of the variables in mode5() and then in mode6() it would automatically assign the value to a variable and make it negative.

For example I would define a1, a2 and s in mode5() as being 1, 2 and 3 and then perhaps in mode6() it would automatically assign different variables b1, b2 and bs as being -2.22, -0.44 and -3.2.

I thought something like this would work?

Code:
Public Const a1 As Single = 1
Public Const a2 As Single = 2   
Public Const s As Single = 3  
Sub Mode5()
 
End sub

Sub Mode6()
    
   ' Automatically assign variables b1, b2 and s as being negative
    Const b1 As Single = -1
    Const b2 As Single = -2   
    Const s As Single = -3    
.........


Would that be possible?

Thank you for your help.
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,792
Office Version
  1. 2019
Platform
  1. Windows
You cannot have local variables and public variables of the same name (or rather, you can, but the local variables will take precedence). So in your above example variable s is a problem.

I guess try either:
Code:
Public Const a1 As Single = 1
Public Const a2 As Single = 2   
Public Const s1 As Single = 3  

Sub Mode6()
Dim b1 As Single
Dim b2 As Single   
Dim t1 As Single

    b1 = -a1
    b2 = -a2
    t1 = -s1  

End Sub

But it isn't too hard to just negate the constants (and I think this is what I'd go with):
Code:
Public Const a1 As Single = 1
Public Const a2 As Single = 2   
Public Const s1 As Single = 3  

Sub Mode6()

    Debug.Print -a1
    Debug.Print -a2
    Debug.Print -s1  

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,335
Messages
5,595,573
Members
413,996
Latest member
mabelO

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top