简体   繁体   English

VBA根据另一个工作表的单元格值选择工作表

[英]VBA to select sheet based on cell value of another sheet

On Sheet 1(viva-2) Row 11 has a drop-down(validation) with yes/no. 在工作表1(viva-2)上,第11行的下拉菜单(验证)为“是/否”。 By default, value will be "no" and sheet 11(Manage-d) cell range A11:D30 should be disabled/locked. 默认情况下,值将为“ no”,并且应禁用/锁定工作表11(Manage-d)单元格区域A11:D30。 Selecting "Yes", user should be able to select Sheet11(Manage-d) and cells from range A11:D30 should be unlocked. 选择“是”,用户应该能够选择Sheet11(Manage-d),并且应该解锁范围A11:D30中的单元格。

I am new to VBA, but I am putting my effort to learn. 我是VBA的新手,但我正在努力学习。

Public Sub Worksheet_SelectionChange(ByVal Target As Range)


    Dim RNG                         As Range


    If Target.Row = 11 Then
     If Range("11").Value = "YES" Then
            Sheets("Manage-d").Select
            Sheets("Manage-d").Range("A11:D30").Locked = False
            Sheets("Manage-d").Range("A11:D30").Activate
        Else
             Sheets("Manage-d").Range("A11:D30").Locked = True
     End If
    End If

Range object represents a single cell or a range of cells.This code is working for me Range对象代表一个单元格或一个单元格区域。此代码对我有用

 If Range("A1").Value = "YES" Then '' Range A1 is the first cell
        Sheets("Manage-d").Select
        Sheets("Manage-d").Range("A11:D30").Locked = False
        Sheets("Manage-d").Range("A11:D30").Activate
    Else
         Sheets("Manage-d").Range("A11:D30").Locked = True
 End If

I use the Worksheet_Change instead of the Worksheet_SelectionChange so that the user doesn't have to ciack another cell to trigger the macro. 我使用Worksheet_Change而不是Worksheet_SelectionChange以便用户不必点击其他单元格即可触发宏。

Assuming the drop-down(validation) is in Range("A11") : 假设下拉列表(验证)在Range("A11")

 Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A11")) Is Nothing Then
        With Sheets("Manage-d")

            .Range("A11:D30").Locked = (UCase(Target.Value) = "NO")

            If UCase(Target.Value) = "YES" Then
                Application.Goto .Range("A11:D30"), True
            End If

        End With
    End If

End Sub

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM