zoukankan      html  css  js  c++  java
  • Excel VBA 根据下拉框单元格的值来改变另一个下拉框单元格的值

    在很多的报表开发中,需要用到VBA去设置Excel的一些规则。 

    以下是一个根据下拉框单元格的值来给特定单元格进行赋值的代码:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        On Error Resume Next:
        Application.ScreenUpdating = False
        If Target.Column = 7 Then        // 这是需要赋值的DDL列
            If Target.Offset(0, -2).Value = 13 Then  //表示赋值列往前移动两个单位的格子值
                With Selection.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                 xlBetween, Formula1:="No"   //赋值为No
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .IMEMode = xlIMEModeNoControl
                .ShowInput = True
                .ShowError = True
                End With
            ElseIf Target.Offset(0, -2).Value <> 13 Then
                With Selection.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                 xlBetween, Formula1:="=Exp"  //这里的Exp为自定义名字的列作为数据来源(Formulas -〉Name managers ) 
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .IMEMode = xlIMEModeNoControl
               .ShowInput = True
                .ShowError = True
                End With
            End If
        ElseIf Target.Column = 8 Then   //下面的逻辑用来控制,当第7列值为No时,会把第8,910,11列保护起来不能输入
            If Target.Offset(0, -1).Value = "No" Then
                Target.Locked = True
                Target.Offset(1, -6).Select
            End If
        ElseIf Target.Column = 9 Then
            If Target.Offset(0, -2).Value = "No" Then
                Target.Locked = True
                Target.Offset(1, -7).Select
            End If
        ElseIf Target.Column = 10 Then
            If Target.Offset(0, -3).Value = "No" Then
                Target.Locked = True
                Target.Offset(1, -8).Select
            End If
        ElseIf Target.Column = 11 Then
            If Target.Offset(0, -4).Value = "No" Then
                Target.Locked = True
                Target.Offset(1, -9).Select
            End If
        End If
        Application.ScreenUpdating = True
         
    End Sub
  • 相关阅读:
    【BZOJ2959】—长跑(LCT维护双连通分量+并查集)
    【BZOJ5394】【Ynoi2016】—炸脖龙(树状数组+广义欧拉定理)
    【BZOJ2588】【Spoj10628】—Count on a tree(主席树)
    SCOI2019爆零记+总结反思
    【SCOI2019】—DAY1T1平台跳跃(打表+高精度)
    省选模板复习—【字符串】
    省选模板复习—【数据结构】
    【BZOJ3572】【HNOI2014】—世界树(虚树+倍增+dp)
    python paramiko 模块
    python sys模块
  • 原文地址:https://www.cnblogs.com/Aaron-Lee/p/9962690.html
Copyright © 2011-2022 走看看