Results 1 to 10 of 52

Thread: ब्लॉग कोशिश कर रहा है بلاگز کی ک*Trying Blogs

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #39
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10

    Positioning of Border between routine sections in VBA. Summary

    Positioning of Border between routine sections in VBA. Summary


    This is an attempt at a short summary, ( Edit: which failed, ….never mind :-) ).
    For more details and justifications see the last two posts.
    ( https://tinyurl.com/yyw85dmg , https://tinyurl.com/yy9rwf85 )
    Summary
    In VBA, in a code module, coding sections, such as procedures and the initial Declaration section, may be directly following on, or may have sections in between which can be ' commented or blank lines , or combinations thereof.

    For the case of coding sections directly following on from each other, the border , ( shown as a continuous light grey line across the code module ) , is in the obvious position
    Code:
    Option Explicit
    Public LudwigII As Legend___________________________________________________________________________________________________________________________________
    Sub  Sub1()
     ‘ hkshh
    End Sub___________________________________________________________________________________________________________________________________
    Sub  Sub2()
     ‘ asskasb
    End Sub___________________________________________________________________________________________________________________________________
    Function  Funnky1()
     ‘   askjhsdh
    End Function
    For the case of separations of 1 or more lines in between the situation is less obvious.
    The simple conclusion from the documentation is that the “comments above a routine belong to the routine below
    With a bit of imagination and lateral thinking, and working somewhat in reverse from knowing the answer, we can accept that, at least loosely.

    The start point is to say that VBA somehow looks up from the top of a routine start code line, ( A routine start is a code line something of the form Sub MySub( ) or Function MyFunction( ) ) . In looking up, VBA does…..
    _(i) attempt to find an “End” , after which a blank line is somewhere after ( looking back down ) .
    _(ii) If it does not find one of those situations, then it stops looking at the “End
    This logic takes care of the simple situations of
    _(i) all blanks in between
    _(ii) all commented lines in between.

    To deal with the mixed case of blank and commented lines we must consider carefully what an “End” is: The first two are obvious, the third is not.
    In all cases they are not necessarily where the border line will be placed: remember the logic:
    ……….. VBA somehow looks up from the top of a routine and
    _(i) attempts to find an “End” , after which a blank line is Present.
    _(ii) If it does not find one then it stops looking at the “End


    What is an “End
    _a) an End ______ type code line: The last line of a routine which is coding: the terminating code instruction.
    _b) The last declaration statement at the top of a module
    _c) Whether by design or accident, VBA “sees” the next line down after a trailing _ as an “End” : Schematically like this:
    '
    ' _ _
    _______This line is seen as anEndcode line ( but may or may not contain the border Line____ )

    '
    '

    ( VBA will stop looking at the first “End” that it finds, even the situation _c) , so we only need concern pourselves with the fisrt trailing _ looking up from a procedure start code line

    _._________________________________________


    Here is just one, very limited, example, showing a slightly extended version of the first example above. This mainly shows the position of the "End"s
    This example shows the effect of a trailing _ _
    But there are other scenarios to consider with and without a _ which can lead to many different positioning of the border line, but which I think all can be explained through the logic discussed in this post. Note, for example, in all the examples below, the border is taken as the "End" line, but that does not need to be the case. It is in those examples below because either the next line is blank or there is no blank to be found.
    The border line may be, but must not necessarily be, on an "End" line.
    Code:
    Option Explicit
    Public LudwigII As Legend '__This is seen as a  b) "End"________________________________________________________________________________________________________________________
    Sub Sub1()
    '  code
    End Sub '____________________This is seen as a  a) "End"___________________________________________________________________________________________________________
    Sub Sub2()
    '  code
    End Sub '_____________________This is seen as a  a) "End"__________________________________________________________________________________________________________
    Function Funnky1()
    ' code
    End Function '_ _ _ _ _ _This would be seen as a  a) "End", but isn't because VBA does not get this far – after looking back up from the procedure below, it mistakes the line after a  _   as an “End.
    
    ' Comments under a Sub
    ' last Comment line _
    _______under a Sub________This is seen as a  c) "End"__________________________________________________________________________________________________________
    
    ' First Comment above a Sub
    ' Comment above  a Sub
    '
    
    Sub Ssenario2()
    '   code
    End Sub 
    
    '


    _.______________________________________________

    Pseudo routine Logic
    Finally a simple pseudo code of the logic. There are probably many different code logics which you could think up. Here is one.
    In Words:
    A prerequisite is to understand my suggested concept of a “End” line.
    The routine starts at the signature or open line or routine Start Line…etc….for example, it starts at this sort of bit: Sub MySub()
    It has a Main Outer Loop which goes up one row at a time, and it keeps Doing that until it finds an “End” line.
    _________ If it finds an “End” then it goes back down line for line in an Inner Loop -- , looking for an blank line, and it keeps Doing that While it has not got back down to the original start point of the pseudo routine
    _________________ If it is at a blank line at any time , in other words it found the fist blank line looking down, .. it jumps out that inner loop and Exit Sub after putting the thin grey border Line ___ in the previous line. So then it does not get further down in this case, and never reaches the original start point of the pseudo routine…. _
    _.....The Inner Loop will not keep going down past the original start point …and if it gets that far then the grey border Line ___ is put in the “End” line. This last bit takes care of the cases of either no blank lines after the “End”, or no lines at all between code sections. If we arrive at this point the pseudo routine ends as we have done all that needs to be done.
    Code:
    Sub Start At_A_Procedure_Start_Line()
     Do ‘ looking for a “End” line ‘ =========Main Outer Loop============================
       Move up a row:- CurrentLine=CurrentLine-1
            If now  at End Sub,  Or at End Function , Or at 1 row down from trailing _ Or at last module top Declare  line then
              Note this line as = “End” line
                 Do While not at Start of procedure ‘ Inner Loop back down to find blank line ----------
                    If current line is Blank then put light grey Border____in previous line : and Exit Sub‘ This will catch the next blank line as belonging to the procedure below ( also catches the case of all blank lines in between )
                  Move down a row ‘       I am moving down .. looking for a blank row
                 Loop ‘ -------------------------------‘ Inner Loop back down to find blank line -------
              At this point we got back as far down as the original start point without finding a blank line
              Put light grey Border___in the  Endline and Exit Sub ‘ This will catch the situation of either no in between rows or all comments, because I get back to the start without finding any blank cells   
            Else
    	‘ We are still looking for a “End” line, which we do by moving up a row  in the outer Loop
            End If 
     Loop ‘ to keep trying to find a “End” line ======Main Outer Loop========================
    DoneIt

    _.__________________________


    If you follow that above logic carefully then, as far as I can tell, it explains all the observed behaviour.
    For detailed examples see the last two posts.
    If you have an example and you are not clear about how your border has come about, then please post a reply here , so that I can take a look to
    _ see if it fits my proposed logic
    _ if it does fit the logic , then I will try to explain that in detail for you

    Alan
    Last edited by DocAElstein; 02-03-2021 at 10:44 PM.

Posting Permissions

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