2014-01-13 186 views
0

我在工作表中有一堆范围名称(大多数是单个单元格)。当我更改名为“Grade”的单元格时,我希望它的值出现在另一个名为“GrdSrchSttng”(6 x 1范围)的范围中作为最后一个值。但是为了做到这一点,我需要知道名为“成绩”的特定单元格发生了变化。 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

就个人而言,使用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

让我们知道这是不是适合或以其他方式。 :)

+0

嗨,谢谢,很好的解决方法。我决定使用这个而不是我的“下一个单元格检查”,因为我更喜欢这个。仍然不明白为什么.name.name不起作用。有点沮丧。但无论如何,非常感谢。 – PBD10017

+0

不客气,谢谢你的接受。 :) – Manhattan

+0

@ BK201我有相同的方法,但在测试期间,我没有得到所需的结果。这张纸上什么都没有发生。所以我没有发布它作为答案。任何想法为什么? :) – L42