Home » excel » excel vba – What is the difference between 'Range.Parent' and 'Range.Worksheet' in VBA?

excel vba – What is the difference between 'Range.Parent' and 'Range.Worksheet' in VBA?

Posted by: admin May 14, 2020 Leave a comment

Questions:

The .Parent and .Worksheet properties, when used with a range, seem to reference to the same worksheet object where Range is located.

For example, both of these lines return the same value:

Debug.Print Selection.Parent.Name
Debug.Print Selection.Worksheet.Name

Is there a difference between the two? Are there advantages/disadvantages to each method?

How to&Answers:

Unless you can guarantee Selection is always part of a Worksheet, it’s not said that Selection.Parent.Name will yield the same result as Selection.Worksheet.Name. If it’s “in” other types of objects (charts or graphics, for example) the result could be quite different – you’d need to do some testing.

So, Selection.Worksheet.Name is more exact (and, as someone has pointed out in Comments, potentially faster in execution). But if you can’t guarantee Selection is going to reference a Worksheet it can trigger an error or yield an unexpected result.