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
相关推荐
boy快快长大9 分钟前
EasyTier初次使用
服务器
MetaLite15 分钟前
Spring Boot 加了 @Valid,嵌套参数为什么还没校验?
java·spring boot·后端
半杯咖啡半行码16 分钟前
QML与 Qt Quick入门实战:从零基础到 C++ 双向通信详解
java·qt·交互
牢姐与蒯19 分钟前
WSL2 迁移教程:把Ubuntu+WSL2从C盘挪到别的盘
linux·运维·服务器·网络·ubuntu·1024程序员节
垆边人似月.22 分钟前
RDMA 文件传输踩坑(二):为什么 `sleep()` 不能保证接收端准备好了?
linux·服务器·网络·rdma
霸道流氓气质23 分钟前
Lingua 语言检测库:n-gram 统计模型、置信度阈值调优与 Spring Boot 分层检测管道的生产级实战
java·spring boot·后端
文丑颜不良啊33 分钟前
LeetCode1281~1300题目解析
java·leetcode
missile11136 分钟前
# HDR 算法 ISP 通路客制化方案(G100 / mt6789)
java·前端·javascript
全栈陈序员1 小时前
【Python】基础语法入门(三十三)—— 日志系统详解:用 logging 记录程序运行
java·开发语言·python
云飞云共享云桌面1 小时前
智能装备工厂研发部门多设计师共享一台SolidWorks服务器做设备装配建模
运维·服务器·网络·人工智能·3d·自动化·制造