返回范围的名称。

时间:2014-01-13 12:23:11

标签: excel vba excel-vba range

我在工作表中有一堆范围名称(主要是单个单元格)。当我更改名为“Grade”的单元格时,我希望其值显示在另一个名为“GrdSrchSttng”(6 x 1范围)的范围中作为最后一个值。但要做到这一点,我需要知道名为“Grade”的特定单元格已更改。 The solution to this question does not work.

我试过了:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim isIn As Boolean
Dim i As Integer
Dim r As Range


Set r = ActiveSheet.Range("GrdSrchSttng")

    If Target.Name.Name = "Search!Grade" Then ' <== this si the line I have the issue with
        Select Case Target.Value
            Case "All"
                r.ClearContents
                r.Cells(1, 1).Value = "All"
            Case ""
                r.ClearContents
                Target.Value = "All"
                r.Cells(1, 1).Value = "All"
            Case Else
                If r(1, 1).Value = "All" Then
                    r.ClearContents
                End If

                i = 1
                Do While r(i, 1).Value <> ""
                    If r(i, 1).Value = Target.Value Then
                        isIn = True
                    End If
                    i = i + 1
                Loop

                If Not isIn Then
                    r(i, 1).Value = Target.Value
                End If
        End Select
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

就个人而言,使用Target.Name.Name有点棘手。我更喜欢使用解决方法,因为它实现了基本相同的结果。除非您特别想要跟踪指定范围的名称,否则我建议您执行以下操作:

Private Sub Worksheet_Change(ByVal Target As Range)
    Whatever = Range("Grade").Address
    If Target.Address = Whatever Then
        Range("GrdSrchSttng").Cells(1, 6).Value = Target.Value
    End If
End Sub

<强> 截图:

设置向上:

enter image description here

编辑Grade时的结果:

enter image description here

如果这是合理的,请告诉我们。 :)