how to improve coding style and move away from hard coding

chopperj

New Member
Joined
May 5, 2010
Messages
11
Hi all, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I have been dabbling on and off in the VBA world for about 10 years, but i have had no real formal training thus developed my own style.<o:p></o:p>
As a recent project will demand lots of VBA i want to approach the coding more intelligently. (in 2003)<o:p></o:p>
With this in mind could anyone advise of any online documents or offer any tips / mindset approaches.<o:p></o:p>
<o:p></o:p>
Also i would be grateful for a explanation of how the VBA architecture ie workbook <o:p></o:p>
worksheet<o:p></o:p>
modules<o:p></o:p>
etc<o:p></o:p>
<o:p></o:p>
I look forward to your responses <o:p></o:p>
<o:p></o:p>
Thank you<o:p></o:p>
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi all, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I have been dabbling on and off in the VBA world for about 10 years, but i have had no real formal training thus developed my own style.<o:p></o:p>
As a recent project will demand lots of VBA i want to approach the coding more intelligently. (in 2003)<o:p></o:p>
With this in mind could anyone advise of any online documents or offer any tips / mindset approaches.<o:p></o:p>
<o:p></o:p>
Also i would be grateful for a explanation of how the VBA architecture ie workbook <o:p></o:p>
worksheet<o:p></o:p>
modules<o:p></o:p>
etc<o:p></o:p>
<o:p></o:p>
I look forward to your responses <o:p></o:p>
<o:p></o:p>
Thank you<o:p></o:p>


For starters, never reference sheets by their name. Always use their codename. This way if somebody changes one of the sheets' name, your macro will still work. You can find the codename for any sheet in the VBA editor. Look at the left hand VBA Project pane where it has all the sheets listed in a fashion like the "Sheet1 (The_Sheet's_Name)" The Sheet1 here is the codename for the sheet named "The_Sheet's_Name".

So instead of writing code like this:

worksheets("The_Sheet's_Name").whatever


Do it like this:

sheet1.whatever


Lastly, figure out how to use the Immediate Window in the VBA editor and also how to use Debug.Print with the immediate window and within your code.
 
Upvote 0
chopperj

I think if you searched the Internet and/or this forum you'll find plenty of advice.

I don't think there is any comprehensive guide to how you should use code - sometimes it can come down to personal choice/opinion.
 
Upvote 0
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Also i would be grateful for a explanation of how the VBA architecture ie workbook <o:p></o:p>
worksheet<o:p></o:p>
modules<o:p></o:p>
etc<o:p></o:p>
<o:p></o:p>

****, I have to finish my article on this, but here are the basics:

Workbook level code is for Workbok level events, like Open, BeforeClose, etc. You can also apply Global events to sheets, like WorkBook_SheetChange in which a single event would apply to all sheets.

Worksheet level code is reserved for each sheet and its respective module. E.G. Controls on only that sheet, or a Change event for just that sheet.

The first two are generally Private and can't be called from the Macro list in Excel.

General/Standard Modules are for generic code that can be called from anywhere.

Forms modules are for code specific to their respective forms.

Then there are Class modules which are a whole 'nother thing.

HTH,
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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
Back
Top