Determining to which code section, comments outside procedures belong in VBA
This story is made up, just for fun. It assumes that the way the things discussed in the last three posts, happen was actually planned… (… it is probably anybodies guess if it was or wasn’t planned…. )
….VBA has an intelligent routine to determine to which code sections, the 'comments between code sections “belong”.…..
Dividing coding section in VBA : Dividing code sections in the VBIDE ( VB Editor )
At the top of a code module you can add things like declaration lines, global variable etc… -- things like
Option Explicit
' Any comment lines you want
Public myGlobalvariable As Long
That above can be considered as a coding section: it is a section of code.
The other types of coding sections are perhaps more obvious, thing like these two coding sections below
Sub mySub1()
Dim strStuff As String
_Let strStuff=”Hello”
_Call myFunctionToSaySomething(strStuff)
End Sub
Function myFunctionToSaySomething(ByVal Messige As String)
Msgbox(Prompt:=”Message was “ & Messige)
End Function
Those 3 sections can all be put in the same code module, ( The declaration section must be at the top ). There can be spaces in between the sections: Any amount of lines can be left in between. Those in between lines can be left blank or filled with
' comments
( Comments are usually ignored by any coding – you can put useful notes there, or write any profanities you choose, or URLs to porno sites etc… etc…. )
Border line______between code sections.
Excel adds automatically a thin grey line, across the code module window, to divide the code sections.
In the example above, if those coding sections were all in the same code module, then VBA adds two thin grey lines: One in between the two procedures and the other between the declaration section and the first procedure.
The VBA editor does not define an “in between” section. Instead, the lines in the space between is divided up between the code sections either side of the thin grey line. The convention is that the line in which the grey line appears to be in is the last line of the coding section above. To help remember this, you can imagine the border line to be made by typing a set of continuous underscores:
________–This line with the underscores in it, is the last line in the coding section above it. In fact if you typed this comment , ' ___ Some comments , as shown below, then you would not be able to see those green underscores as they get hidden in the grey border line which VBA adds to separate the code sections.
Option Explicit
Public LudwigII As Legend
Sub Sub1()
' code
End Sub ' ___ Some comments
Sub Sub2()
' code
End Sub
If you copied the above to an empty code module, then you would actually see, something approximately like this below
UnderscoresHiddenInLightGreyBorder.JPG : https://imgur.com/3kkjLW4
Option Explicit
Public LudwigII As Legend__________________________________________________ _______
Sub Sub1()
' code
End Sub '____Some comments __________________________________________________ _____
Sub Sub2()
' code
End Sub
( The underscores which you may have added, ___ , are there, - they are just “hidden behind” the grey border line )
Note: it is important in the above experiments to add some comments after a single underscore , or alternatively use more than one underscore. This is because a singly isolated trailing underscore , __ _
, can give interesting results, as we will discuss.
Intelligent routine to determine to which code sections, the 'comments between code sections “belong”.
Although the in between lines must belong to the upper or lower coding section, we can determine to a large extent to which. In other words, we can determine to a large extent where Excel places the long light grey border line_________
Excel uses an intelligent routine: Initially, as default, this is based on reasonable assumptions as to how you might typically lay out lines which you wanted to “belong” above or below. In addition an extra feature allows you to influence slightly, the final choice made by Excel.
Here is a summary of how the intelligent routine behaves:
How Excel determines where to put the____light grey border that divides coding sections in a code module
Situation 0a) All lines between used as comments.
Excel will assume you are not wanting to divide the comments up, as you have made no attempt to separate them. It will assume that it is more likely that these comments are intended to belong to the following code section. Correspondingly you will get this sort of behaviour in the lines in between:
End Sub__________________________________________________ _________________
' Comment
' Comment
Sub Hello()
Situation 0b) All lines between are blank
Excel remains at the first default of Situation 0a)
End Sub__________________________________________________ ______________________
Sub Hello()
Situation 1a) Simple split of comments
You divide your comments by one or more consecutive empty lines, as you probably would for neatness if the lines above should belong to the coding section above, and the lines below to the next coding section. Excel positions the border appropriately
End Sub
' Comment for above
' Comment for above__________________________________________________ ______________________
' Comment for below
' Comment for below
Sub Hello()
End Sub
' Comment for above
' Comment for above__________________________________________________ __________________
' Comment for below
' Comment for below
Sub Hello()
Situation 1b) Simple split of comments , lower section has blank lines
After the initial blank separating line, or lines, any more below are left as blank lines
End Sub
' Comment for above
' Comment for above__________________________________________________ ___________________
' Comment for below
' Comment for below
Sub Hello()
Situation 2 Upper comments section has blank lines , lower section has blank lines
Situation 3 Upper comments section has blank lines , lower sections has all comment lines (no blanks), or lower section has all blank lines
Excel has a feature to allow us to do this. We have some flexibility in how to use it. It works as follows: Considering all the examples above, …. we note that Excel saw the …_
End Sub
_... as signalising the last code line of the coding section above.
The extra feature is that we can use either of these 2 possibilities within our in between section, the important part is an isolated trailing underscore _ _
' comment _
Empty line
' comment _
' comment
The effect of these is such that Excel will act in regard to the positioning of the__border line the same way as it would have, had it have seen the following in the second of those two line pairs above
' comment _
End Sub
' comment _
End Sub
The end result of this is that we can
_ have any combination of comment or blank lines above this point: Excel will no longer concern itself with those lines in terms of border position considerations: Those lines will always “belong” to the coding section above.
_ The lines below that effective End Sub will be subject to the same default rules already discussed. This allows us to make any combination of blank lines and comment lines below this point.
As far as I can tell, the only situation not possible to obtain would be to have more than one last empty lines belonging to the procedure above. All other situations should be possible. But I have not done lots of testing yet…
Here just a few final examples using the isolated trailing underscore _ _
In understanding how they come about you need to consider the furthest down isolated trailing underscore _ _
, as effectively making the line below it look like End Sub
Code:
End Sub
' Comment for above _
' Comment for above________________________________________________________________________________
' Comment for below
' Comment for below
Sub Hello()
Code:
End Sub
' Comment for above
' Comment for above _
_________________________________________________________________________________________
' Comment for below
' Comment for below
Sub Hello()
Code:
End Sub
' Comment for above
' Comment for above _
____________________________________________________________________________________________________
' Comment for below
' Comment for below
'
' Comment for below
'
Sub Hello()
Code:
End Sub
' Comment for above
' Comment for above _
___________________________________________________________________________________________________
Sub Hello()
Here below are those last 4 examples again, using the idea of the effective End Sub. If you then concentrate on the lines below this and consider them based on the original default rules, then the behaviour of the_______positioning is consistent with those default rules.
Code:
End Sub
' Comment for above _
End Sub
' Comment for above_______________________________________________________________________________
' Comment for below
' Comment for below
Sub Hello()
Code:
End Sub
' Comment for above
' Comment for above _
_End Sub__________________________________________________________________________________________
' Comment for below
' Comment for below
Sub Hello()
Code:
End Sub
' Comment for above
' Comment for above _
_End Sub___________________________________________________________________________________________________
' Comment for below
' Comment for below
'
' Comment for below
'
Sub Hello()
Code:
End Sub
' Comment for above
' Comment for above _
_End Sub__________________________________________________________________________________________________
Sub Hello()
I won’t labour the point with more examples, but conclude with saying that you can use the extra feature, along with a knowledge of the default way that Excel behaves, to get the__border line at most positions, regardless of how you have your in between lines filled or not filled.
http://www.eileenslounge.com/viewtop...247408#p247408
http://www.excelfox.com/forum/showth...ll=1#post11016
Bookmarks