How do you get a Range to return its Name?

后端 未结 2 393
无人及你
无人及你 2020-11-27 07:29
Dim sampleRange as Range
Set sampleRange = Worksheet.Range(Cells(1,1),Cells(1,4)
sampleRange.Name = \"Range1\"
MsgBox sampleRange.Name

The above co

2条回答
  •  醉话见心
    2020-11-27 07:56

    sampleRange.Name = "Range1" to name the range is poor practice

    It only works because Name (the string containing "The Name") is the default property of Name.

    Much better sampleRange.Name.Name = "Range1"

    It's never good practice to use the default property without referring to it explicitly.

    Good programming exactly controls the environment by referring explicitly to the property required.

    Removes ambiguity as created the issue here. Eliminates the issue created if the default property is changed in a future update.

提交回复
热议问题