VBA Range.Insert Method ( Excel OOP Syntax Error )
( cross posted here: http://www.mrexcel.com/forum/general...ml#post4501533
)
A general discussion on the Excel ( Range ) .Insert Method with emphasis on both the optional argument ( xlShift bit ) .. and a suggestion that it is not really well described as a Range Method
OK. So many people know all this.. maybe.. But there are some subtle points i discuss here...
The OOP ( Object Orientated Programming ) Period ( .Dot ) stuff convention / line syntax confuses me a bit here...
And the documentation never made it clear to me what the optional argument did. And that “Microsoft Excel decides based on the shape of the Range where to shift” for no specified argument, is rather worrying / and uncertain IMO.
I agree it is all there and with hindsight it does makes sense, “.......shifts other cells away to make space…….“
Initially however i think the way the thing is presented is misleading....
But the nice thing of the Mr excel Forum Screenshot tools is that it makes it nice to and easy to demonstrate this, using the background colours...
Let me try to explain in words first.
Here is the current ( “pseudo”) code line for the VBA “Range” Insert Method.
ARange.Insert ShiftDirection
According to my understanding of OOP, .Insert is “applied” to the Range Object, ARange, in the same way that, for example, the
ARange.Clear
Property would be applied to clear most things done to a Range.
Similarly, applied to a Range Object, something like .Value Property Returns the Value ( or Values ) of the Range
= ARange.Value
Similarly the inclusion ( often omitted / forgotten ) of Let will prepare VBA to “give” the Range Object a value
Let ARange.Value = ARange.Value
In a Similar way applying the .Address Property of a Range Object can be used in a code line Such as to give the Range Values the full Address of the complete Range
Let Range.Value = ARange.Address ' Address held by Excel for this particular Range Object will be returned as the Range Value in every Cell ( or in memory allocated space for Value of that Range Object )
So generally we are talking about things to do with that Range object giving ( Letting ) or taking (getting ) from it.
Very Briefly that is OOP , ( I think ).
In the case of the
.Insert
, I am not sure if these ideas are valid.. In the demo below I have prepared I believe it is more true or appropriate to say the following.
“At a specified Area of the spreadsheet you do the following:
________ Spread / shift apart a space in a ( by argument specified direction ) and produce a new “Virgin” un referenced Range Object. The “Range” specified serves conveniently to say both where that space will be made and necessarily also the size of the new range object.”
Clearly looking at in that way, The Range Object used is not itself directly being affected. Indirectly it is itself shifted or Offset, but so are typically a large amount of Range Objects in this case, and only coincidentally is this range Object one of them
So in this particular Method I think it should be written the other way around, and it should be a Method of the Worksheet, or an Application Method. “Pseudo” Codes:
Wonkation.Insert _ ( RangeTemplate:=ARange, ShiftDirection:=Down / ToRight )
Wonkation.Shift _ ( RangeTemplate:=ARange, ShiftDirection:=Down / ToRight )
Wonkation.OffsetShift _ ( RangeTemplate:=ARange, ShiftDirection:=Down / ToRight )
Or, even name it differently
Wonkation.NewVirginRangeCreate ( WhereToAndNewRangeSize:=Range, WhichDirectionToShiftToMakeSpaceForIT:=Right Or Down )
Or, Function like
Wonkation.AlnWnkShtIst ( ARangeArea As Range, ShtSpread As String )
Later in this Thread I suggest an alternative to this line
rng.Insert xlShift_____
based on a FUnction I have written and share of this corresponding form, and which would be callled thus: ( hence replacing the above line )
Call AlnWonkShtIst(rng, ______ )
_..................................
I have now a Demo ( accompanying code given at end.. )...... Initially the main Code bits and Lines summarised. ( The code is best stepped through in debug F8 mode whilst viewing the Spreadsheet
..........
_................................
Bookmarks