+ Reply to Thread
Results 1 to 7 of 7

Good Links that Discuss Proper Programming Technique

  1. #1
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Good Links that Discuss Proper Programming Technique

    Yes I know.....google it. I have and I can't seem to find anything that explains when to put code in modules and when it should go in the "thisworkbook" object or in a worksheet object.......

    Here's the thing. I am writing my first vba program (well actually first program ever that actually does something useful). The objective is to take investment mean, stdev, and covariance assumptions and generate a number of efficient frontiers using this data. Then it plots a number of different target portfolios and runs Monte Carlo simulations on each target portfolio taking into account cash flows in and out of the portfolio. Then it uses that information to create a number of different worksheets that it populates with data and pretty charts. The idea is that anyone in my company can then put in their own assumptions, run the program, and out pops a perfectly formatted and ready to pdf report. A task that took a couple of days could now conceivably be done in a few minutes.....of course we would still charge the clients as if it took a few days. So far it has surprisingly come together pretty well and most of the pages of the report can now be generated with the click of a button. Here's the thing. When I go into the code it is a complete mess. One sub after the other all on the "ThisWorkBook" Object. I am sure that it isn't supposed to be a jumbled mess. Is their a sticky on this forum that explains how to use modules effectively to get rid of some of the clutter?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Good Links that Discuss Proper Programming Technique

    I think if you polled the people that respond regularly on the forum, you'd find most have learned gradually by experience and repetition, by spending time on this and other forums, and supplemented by occasional books; Excel just has a pretty large object model.

    I've heard VBA for Dummies is not bad. Walkenbach's Power Programming series is very good, but parts are a little beyond beginner's level.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Good Links that Discuss Proper Programming Technique

    There are books on the subject, located via amazon search.
    http://www.amazon.co.uk/Structured-F...7499698&sr=1-9

    I would second shg's suggestions of the VBA dummies if you are just starting
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Good Links that Discuss Proper Programming Technique

    It sounds like none of the code should be in ThisWorkBook
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Good Links that Discuss Proper Programming Technique

    I'm just working my way through

    Steve Roman's Writing Excel Macros with VBA from O'Reilly

    http://www.amazon.com/Writing-Excel-...7501328&sr=8-9

    Although it's for Excel 2002, and there does not seem to be an edition for a later release, I find it quite useful. The structure works for me and the examples are well chosen and deal with the stuff I'm interested in.

    I've also had a closer look at
    Microsoft Excel VBA Programming for the Absolute Beginner by Duane Birnbaum and Michael Vine , but was not too impressed. The examples are all around programming games and creating user interfaces on top of Excel, but very little about manipulating user input/output, spreadsheet components etc.

    hth

  6. #6
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Good Links that Discuss Proper Programming Technique

    I actually got started using VBA for Dummies and found it very helpful. The rest I have learned through trial and error and lots of google searches. The fact that my entire program is in "ThisWorkbook" and royUK above suggests that it probably shouldn't be means that I really need to learn the proper way to use the developer environment properly and to put the pieces of code where they are supposed to go. I guess I probably need to break out each sub into its own module but that is purely guessing on my part.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Good Links that Discuss Proper Programming Technique

    I keep meaning to get a book myself as I'm also self taught (lots (lots) of trial and error) and I often feel my lack of knowledge on the fundamentals catches me out at times... I've been recommended PED but that's pretty advanced I think so I may opt for something a little more middle-ground.

    Regards your comment...

    The fact that my entire program is in "ThisWorkbook" and royUK above suggests that it probably shouldn't be means that I really need to learn the proper way to use the developer environment properly and to put the pieces of code where they are supposed to go. I guess I probably need to break out each sub into its own module but that is purely guessing on my part.
    I always tend to get my terminology mixed up because I try to categorise things in such a way as they make sense to me rather than concentrating on them being "technically correct"... so please forgive any technical inaccuracies in the remainder... but basically I would say - try to think of a VBE project in terms of being a collection of Objects & "standard" Modules... (ignoring Class Modules/UserForms etc... for the time being)

    In general the Objects in VBE are ThisWorkbook, Sheet1 etc
    In an XL file Worksheets are Objects, the Workbook itself is an Object.

    On that basis I (and this personal preference) try and keep these Objects in VBE clear of code for all but Object driven Events ...
    ie ThisWorkbook would contain only code related to Workbook Level events (Open, Close, generic Sheet event etc...)
    Sheet1 would only contain code specific to Events occurring on Sheet1 (Change, Calculate, DoubleClick etc...)

    Chip Pearson (surprise surprise) has a nice page on the subject of Events: http://www.cpearson.com/excel/Events.aspx

    All other non-event driven code would reside in standard Modules (inserted via Insert Menu).

    Regards my non-event driven code residing in Modules - I generally keep my Modules to the odd routine here and there (else they can be a nightmare to debug with so much code going on in one Module - not that mine ever debug of course!). On that basis I try to keep the Modules specific to a given task ... if I have 2 routines related to the same task they may share the Module.
    Last edited by DonkeyOte; 11-07-2009 at 11:21 AM. Reason: typos

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1