PDA

View Full Version : [SOLVED:] Parent vs Worksheet



GarysStudent
02-18-2015, 09:29 AM
For the Range Object, what is the difference between the Parent property and the Worksheet property:


Sub dural()
Dim r As Range
Set r = ActiveCell
MsgBox r.Parent.Name
MsgBox r.Worksheet.Name
End Sub

snb
02-18-2015, 10:07 AM
I don't think there's any difference.

ZVI
02-19-2015, 03:07 PM
Property Parent provides Late Binding connectivity to the object library (blind for debugging).
Property Worksheet provides Early Binding, thus a bit faster, supports Intellisense, represents the object library in Object Browser (easier to debug).

Paul_Hossler
02-20-2015, 06:38 AM
The Excel Object Model is kind of like a tree structure with each entry having a containing object called the parent.

So .Parent have .Parent have .Parent, etc.




Option Explicit

Sub dura2()

Dim r As Range
Set r = ActiveCell
MsgBox r.Parent.Name
MsgBox r.Parent.Parent.Name
MsgBox r.Parent.Parent.Parent.Name

End Sub





I find it useful when writing general purpose modules like a sample below.

Regardless of which WS the input range is actually on, .Parent will return the appropriate WS object




Sub CenterHeader(Optional r As Range = Nothing)
If r Is Nothing Then Set r = ActiveCell

r.Value = 123456

With r.Parent.PageSetup
.CenterHeader = "Demo"
End With
End Sub