EXCEL VBA限制工作数据批号或者自定义规则完整

EXCEL VBA限制工作数据批号或者自定义规则完整

python 复制代码
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim nRow%, Arr(), cMc$, cPc$, cTxt$, nSum!
    If Target.Row = 1 Or Target.Column <> 4 Then Exit Sub
    If Target.CountLarge > 1 Then Exit Sub
    cMc = Target.Offset(0, -1).Value
    cPc = Target.Value
    If cMc = "" Or cPc = "" Then Exit Sub
    For sh = 0 To 1
        With Sheets(Array("期初", "入库")(sh))
            nRow = .Range("a1048576").End(xlUp).Row
            Arr = .Range("a1:e" & nRow).Value
        End With
        For i = 2 To nRow
            If Arr(i, 2 + sh) = cMc And Arr(i, 3 + sh) = cPc Then
                nSum = nSum + Arr(i, 4 + sh)
            End If
        Next
    Next
    nRow = Target.Row - 1
    With Me
        Arr = .Range("a1:e" & nRow).Value
    End With
    For i = 2 To nRow
        If Arr(i, 3) = cMc And Arr(i, 4) = cPc Then
            nSum = nSum - Arr(i, 5)
        End If
    Next
    With Target.Offset(0, 1).Validation
        .Delete
        .Add 2, 1, 8, nSum
        .InputTitle = "最大值"
        .InputMessage = nSum
    End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim nRow%, Arr(), cMc$, cTxt$, sh%
    If Target.Row = 1 Or Target.Column <> 4 Then Exit Sub
    If Target.CountLarge > 1 Then Exit Sub
    cMc = Target.Offset(0, -1).Value
    If cMc = "" Then Exit Sub
    For sh = 0 To 1
        With Sheets(Array("期初", "入库")(sh))
            nRow = .Range("a1048576").End(xlUp).Row
            Arr = .Range("a1:d" & nRow).Value
        End With
        For i = 2 To nRow
            If Arr(i, 2 + sh) = cMc Then
                If Not cTxt & "," Like "*," & Arr(i, 3 + sh) & ",*" Then
                    cTxt = cTxt & "," & Arr(i, 3 + sh)
                End If
            End If
        Next
    Next
    With Target.Validation
        .Delete
        If cTxt <> "" Then .Add 3, 1, 1, cTxt
    End With
End Sub
相关推荐
吴冰_hogan19 分钟前
Java虚拟机(JVM)的类加载器与双亲委派机制
java·开发语言·jvm
程序员shen16161120 分钟前
注意⚠️:矩阵系统源码开发/SaaS矩阵系统开源/抖音矩阵开发优势和方向
java·大数据·数据库·python·php
菜鸟康38 分钟前
Linux系统编程——理解系统内核中的信号捕获
linux·运维·服务器
张明奇-琦玉40 分钟前
Boost之log日志使用
linux·服务器·算法
青春男大43 分钟前
java队列--数据结构
java·开发语言·数据结构·学习·eclipse
yzhSWJ1 小时前
mybatisplu设置自动填充
java·spring·tomcat
Java雪荷1 小时前
基于 Vant UI + Redisson BitSet 实现签到日历
java·redis·vue
白宇横流学长1 小时前
基于Javaee的影视创作论坛的设计与实现【源码+文档+部署讲解】
java·java-ee
MaoSource2 小时前
Debian 12.0安装nginx
服务器·nginx·debian
南宫生2 小时前
力扣-数据结构-4【算法学习day.75】
java·数据结构·学习·算法·leetcode