PDA

View Full Version : Clean up sloppy, repetitious code



Coolmo
2004-10-25, 07:42 PM
Here's a quick and easy one.....

I've written some pretty powerful programs in VBA but I'm sure they would be considered sloppy to some people because I repeat processes often. Can someone enlighten me on how to have a Private Sub go out to another part of the program to make some calculations and then return the variables back to the same Private Sub and continue working? Do I put the calculation portion of this in a module? Do I have to make all variables Public? Can I have the calculation portion query other objects on the form and return values? Etc.....

Can someone write out some quick code that I can use as example or point me to a link for this info?

Thanks in advance!

chatcher
2004-10-25, 09:02 PM
Generally speaking, It's better to avoid public variables.

You can place a public function in a module and it can be used across the entire project:
In simple form:

Public Function CalcNmber(dIn As Double) As Double
dTemp As Double
dTemp = dIn * 2.25
CalCNumber = dTemp
End Function

Then in another module method, function, etc:

dim dMyDouble as double
dMyDouble = CalcNumber(12.345)

can be used as many times as you need.

A public function in a standard module is accessible to all code in the project.