+ Reply to Thread
Results 1 to 11 of 11

Follow-up question about variables [Private]

  1. #1
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Follow-up question about variables [Private]

    Hi

    A few weeks back I had some silly questions about variables. Now I have another.

    When using Private for my variables to be shared throughout my Sub routines. Should I still be setting them to nothing at the end of each Sub. Then redefining them at the beginning of the Next Sub that will be using whatever variables.

    Please Login or Register  to view this content.

    Am I doing that correctly? Or once I set the variables should I just leave them until the end of my entire macro then set them to nothing on the last Sub routine?

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Follow-up question about variables [Private]

    If you're setting them to nothing at the end of each sub, then they shouldn't be module scoped but function/sub scoped. Scope should always be kept as small as possible, it makes code easier to follow and reduces bugs.

    Normally it is a waste of time setting variables to nothing and is superflouous

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Follow-up question about variables [Private]

    Using variables like this is not the best programming practice. To do what you describe it is better to declare what you need within each Sub. If the Subs need to share data, it is better to pass data as parameters.

    In the relatively few cases where it is justified to use common data, you would not initialize at the beginning of each Sub and set to Nothing at the end; it contradicts the justification.

    Using common data creates an undesirable form of tight coupling. It creates dependencies among Subs that are not explicit and complicates debugging and maintenance.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Follow-up question about variables [Private]

    Hi xlbunny,

    Back in some programming class a few decades ago we used to watch the "stack" being built with each new Subroutine. At the end of the subroutine the stack was cleared and all the variables for that Sub were lost. We call that "Scope of a Variable". Read more about them at:
    http://www.cpearson.com/excel/Scope.aspx and
    http://www.excel-easy.com/vba/exampl...ble-scope.html

    In Excel it is also very important to know where your code lives. If it lives behind a worksheet it is bad form to have it do stuff to anything outside that sheet. If it lives behind a userform then it should only do stuff on that userform. This isn't always true but gives the idea that code functions with a SCOPE of what it can deal with. I've found that creating Global Variables, that can be seen and used by all code is sometimes better than trying to pass them from one Sub/Module to another. ALSO - you can put something into a cell on a worksheet and retrieve it just like using a variable.

    Hope this helps.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Follow-up question about variables [Private]

    Hi Kyle, and 6String thanks.

    Yeah I have to agree with you guys on that I do have a bug closing a workbook & setting it to nothing makes Excel Crash. But if I just close the workbook no problems.

    What I was trying to do was make workbook a constant so I didnt have to go through the processes of discovering every time I needed it. It has a dynamic name in a dynamic folder.

    The only thing consistent about the folder or file is the string HBDC in the beginning of each.

    Taking from what Kyle said about scope I should maybe do a UDF that will do the discovery of the folder and the file for me and use that in the Sub's.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Hi Marvin thanks for the information about Stack I heard the term but had no idea. Other than it looked like something complicated and something I wanted to stay away from. Maybe Im ready now because everything you just said made sense to me and doesnt sound intimidating but rather intriguing. Thanks for sharing the links I will check them out ASAP. I didnt overlook the fact that, thats the 2nd time you told me about Global variables Marvin. I am going to see where I can start fitting them in to this code.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Thanks everyone for taking the time to read this and sharing your knowledge.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Follow-up question about variables [Private]

    Quote Originally Posted by MarvinP View Post
    I've found that creating Global Variables, that can be seen and used by all code is sometimes better than trying to pass them from one Sub/Module to another.
    It might be easier to code, but not necessarily better. Although as I mentioned, it has its place. In 1979 Edward Yourdon published a seminal work on software design, a large part of which addressed coupling and cohesion. Coupling addresses how intertwined the dependencies are between two modules. The objective is to have loose coupling, so that modules can be as independent as possible. Common data coupling is towards the other end of the spectrum, because to understand how to use global data in one module, you have to understand how the global data is being used everywhere. Global data can be used very effectively when all the code is written by one person and will never have a bug and never have to be updated. But in most situations it can be difficult to debug and difficult to modify without introducing bugs.

    A good (but unattributed) digest of Yourdon's analysis can be found at http://courses.cs.washington.edu/cou...-cohesion.html

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Follow-up question about variables [Private]

    $116 is a little steep for a book about programming. I look at more as common sense and learning from my (and others) mistakes. I used to use I, j and k a lot for variable names but now use variables like "LastRow" and "RowCtr" so I can understand my code without needing to look up at the DIM statements. There was a push a few decades ago to use standardized prefixes for all variables as published by Reddick. After trying to do that for a few years, I gave up as it interfered with my working with the code instead of dealing with Reddick. I now like to read John Walkenback's code as it seems to simply make more sense to me.

    I believe different people code with different styles. I grew up with a book called "The Art of Software Testing" which pushed the idea that programming is an art as much as a science. From this I learned that good code can be read and understood a year or two after writing it, while bad code needs a lot of hard work to understand.

    Good code is like a good mystery novel. It keeps things moving and fits together correctly. It ties up loose ends without sending you to another series to find out what should happen elsewhere. To the point I don't like "Exit Sub" in the middle of code. I like to everything stop at the bottom of my code.

    I've never seen the distinction between Coupling and Cohesion before but completely understand where it came from and why. Thanks for pointing me to these articles.

  8. #8
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Follow-up question about variables [Private]

    Hi

    Heres some of my homework. JK but this is how I think Im gonna try to Code my userform. I know I am still doing the same thing, but it is a little differant in that I am defining whatever in a function and passing it to the Sub. I like the idea of the Module level variable because I had maybe 3/4 different variables throughout my entire project & it was all the same workbook now add another 3 workbooks to that and I have a nightmare of variable madness. With the Module level variables they are all the same x or y. (Just realized the 2 known workbooks & other knowns should be constants.)

    Like Kyle said to have Module level variables & set them to nothing kinda or does negates the purpose entirely. I was just doing that because thats what I learned to to at the end of a Sub. But like I said
    I do have a bug closing a workbook & setting it to nothing makes Excel Crash.
    So Im not gonna set anything to nothing till the last Sub and maybe not even then. Just unload the user form and close the workbook it lives in. Im doing all of this remotely so the instance that instantiates the form doesnt pickup any nasty stuff from whatever the form does or is doing.
    What I mean is when I run my code locally Excels memory ramps up to half a gig and never releases it till I close Excel. By doing it remotely I avoid all that. Well it still happens it just doesnt matter as much when we are really working from somewhere else.
    Please Login or Register  to view this content.
    Yeah JohnW is great Chip too. Im gonna see if I can get that book and check it out thanks Jeff

    To Kyles point.
    Please Login or Register  to view this content.
    Last edited by xlBunny; 04-23-2015 at 04:55 PM.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Follow-up question about variables [Private]

    I am not suggesting that anybody buy that book!!!

    I got that book in 1979, my first year as a professional programmer. The only reason I mention it is that it is a historical artifact that was a very important work at the time, and its principles are still valid. But it's not a programming book--it's very much a theoretical book about design.

  10. #10
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Follow-up question about variables [Private]

    Hi,

    Heres the Book (CliffNotes I guess) : Structured Design: Fundamentals of a Discipline of Computer Program and System Design
    Prentice-Hall, 1979 (Facsimile edition 1986).
    ISBN 0-13-854471-9
    Last edited by xlBunny; 04-23-2015 at 06:10 PM.

  11. #11
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Follow-up question about variables [Private]

    Creating Simpler Statements with Named Arguments

    For many built-in functions, statements, and methods, Visual Basic provides the option of using named arguments as a shortcut for typing argument values. With named arguments, you can provide any or all of the arguments, in any order, by assigning a value to the named argument. You do this by typing the argument name plus a colon followed by an equal sign and the value ( MyArgument:= "SomeValue") and placing that assignment in any sequence delimited by commas. Notice that the arguments in the following example are in the reverse order of the expected arguments:

    Please Login or Register  to view this content.
    This is especially useful if your procedures have several optional arguments that you do not always need to specify.
    I think this is exactly what I was looking to do all along. Winner winner chicken dinner....

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Follow Hyperlink question.
    By JO505 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 01-24-2013, 11:41 AM
  2. Follow up question on website
    By mikejones in forum Excel General
    Replies: 1
    Last Post: 02-23-2011, 11:16 AM
  3. Private modules and global variables
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2005, 03:15 PM
  4. [SOLVED] Class Modules and Private Variables
    By Joe Cletcher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2005, 03:20 PM
  5. Follow-Up (Clarification) to MIN question
    By Odawg in forum Excel General
    Replies: 4
    Last Post: 10-20-2005, 12:05 AM

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