Excel动态下拉菜单转到Cell

时间:2014-09-04 18:43:32

标签: excel vba excel-vba excel-2013

Private Sub Worksheet_Change(ByVal Target As Range)

    Select Case Range("B1").Value
        Case Is = "G&A Total"
        Application.Goto Range("G_A_Total"), Scroll:=True

        Case Is = "Accounting"
        Application.Goto Range("Accounting"), Scroll:=True

        Case Is = "Environmental"
        Application.Goto Range("Environmental"), Scroll:=True

        Case Is = "GSC"
        Application.Goto Range("GSC"), Scroll:=True

        Case Is = "Human Resources"
        Application.Goto Range("Human_Resources"), Scroll:=True

    End Select

End Sub

我目前有这个代码可以使用数据验证下拉列表。我想要知道的是,无论如何要使这个动态变化,以便我放入列表中的任何东西都变成它所取代的东西而不必手动输入每个。

下面的当前编辑。

Private Sub Worksheet_Change(ByVal Target As Range)

    Select Case Range("B1").Value
        Case Is = Range("A1")
        Application.Goto Range("G_A_Total"), Scroll:=True

        Case Is = Range("A2")
        Application.Goto Range("Accounting"), Scroll:=True

        Case Is = Range("A3")
        Application.Goto Range("Environmental"), Scroll:=True

        Case Is = Range("A4")
        Application.Goto Range("GSC"), Scroll:=True

        Case Is = Range("A5")
        Application.Goto Range("Human_Resources"), Scroll:=True
    End Select

End Sub

我更改了特定的Case是要引用由数据验证选择的单元格的单元格引用而不是引用每个单独的单元格是否有一种方法来命名它们然后引用范围名称?

也许像这样的东西

    Select Case Range("B1").Value
            Case Is = Range("Departments")
            Application.Goto Range("DepartmentsGoTo"), Scroll:=True                
        End Select

然后有2个远程命名列,我可以更新以避免经常更改代码?

1 个答案:

答案 0 :(得分:1)

基本上,将每个Named rangeB1

中的值进行比较
Private Sub Worksheet_Change(ByVal Target As Range)

For Each myName In ActiveWorkbook.Names    
    Select Case Range("B1").Value
      Case myName.Name
        Application.Goto Reference:=myName.Name, Scroll:=True
     End Select
Next

End Sub