General tips for good code formatting

N

NDBC

I finally have my code working. I am trying to tidy it up for several reasons

1) so others can follow it
2) so it will run faster
3) so it is easier/quicker to edit
4) to reduce the risk of typing mistakes

So far I have implemented the option explicit statement and put in some
loops for repetitive code. I have also saved it as a web page and then saved
it back to an xls file to reduce file size (went from 9MB to 5MB). Is there
any risks with doing this.

Is there a website I can look at that outlines basic good practice for code
format.

Thanks again for everybodies input. My scoring program already far exceeds
my expectations of what I was aiming for when I first started. Unfortunately
the time taken to do it was the same.
 
J

Jim Cone

I hope you don't have a module that is 5mb. <g>
64kb seems to be a popular limit on size for a module.
(you can export it to a folder and check the size)

Lots of comments in your code explaining the why, not just the what,
will be helpful to you later. A few months from now, chances are
you won't understand the code unless you added comments to it.
Ken Getz ran some tests (several years ago) and stated that code
runs just as fast with comments as without.

Do you reset everything you altered before exiting?
(events/calculation...)
Are all the forms unloaded?
Do you have/need error handling?
Have you minimized the use of Global variables?
Does it compile?

Try...
Copying your code out to Notepad.
Delete the module.
Insert a new module.
Copy the code from Notepad and paste into the new module.
(may reduce your file size and clean things up some)
--
Jim Cone
Portland, Oregon USA



"NDBC" <[email protected]>
wrote in message
I finally have my code working. I am trying to tidy it up for several reasons

1) so others can follow it
2) so it will run faster
3) so it is easier/quicker to edit
4) to reduce the risk of typing mistakes

So far I have implemented the option explicit statement and put in some
loops for repetitive code. I have also saved it as a web page and then saved
it back to an xls file to reduce file size (went from 9MB to 5MB). Is there
any risks with doing this.
Is there a website I can look at that outlines basic good practice for code
format.
Thanks again for everybodies input. My scoring program already far exceeds
my expectations of what I was aiming for when I first started. Unfortunately
the time taken to do it was the same.
 
N

NDBC

Thanks Jim,

My biggest module is 41kb, so at least that part seems ok. I have 20
worksheets with varying amounts of formulas in them so I'm guessing that's
where the file size is coming from.

I already have a fair few notes in there as well but I might put a few more
in with regards to the why.
 
J

Jacob Skaria

Congrats!!!...Check out the below link on coding practices.....(will be useful)

http://blogs.msdn.com/excel/archive/2009/03/12/excel-vba-performance-coding-best-practices.aspx

Below are some other tips for a starter level....

--As Jim mentioned include lot of comments. I personally include comments
for each variables I declare; I had experiences spending hours to identify
what the variable do..

intRaceCounter 'Number of races
dtStartTime 'Start time of race

--Using data type prefix for variables

lngRow denotes data type Long
varTemp denotes that the variable is of Variant data type
strRaceName denotes that the variable is String variable
arrData denotes that the variable is an Array

User forms with frmStartUp, frmCalculations, frmSettings
Textboxes prefixed with txtStartTime, txtEndTime
Comboboxes with cmbStartTime, cmbEndTime
Command Buttons with cmdSubmit, cmdOK

--Naming of variables..While coding all of us prefer to use short variables
names eg (x,y,c,i are commonly used) but in the long run that will not
help..Name it as below so as to reflect the variable type and the purpose...

strFirstName
dtStartDateTime
dtStopDateTime


If this post helps click Yes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top