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
相关推荐
2301_781392523 分钟前
MySQL格式化数据展示——分页查询
java·数据库·mysql·性能优化
Jorunk5 分钟前
如何配置实验室的服务器
运维·服务器
Java后端的Ai之路19 分钟前
【Java教程】- 并发编程核心知识解读
java·开发语言·并发编程
椰羊~王小美23 分钟前
为什么@Builder 注解默认父类字段不可见
java
一 乐24 分钟前
学生宿舍管理|基于springboot + vue学生宿舍管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·助农电商系统
一人の梅雨25 分钟前
义乌购商品详情接口进阶实战:批发场景下的精准解析与高可用架构
java·服务器·前端
Dontla29 分钟前
Mybatis Introduction (Java ORM Framework)
java·开发语言·mybatis
信码由缰31 分钟前
JExten:基于Java模块系统(JPMS)构建健壮的插件架构
java·开发语言·架构
NuageL34 分钟前
SpringBoot使用@Scheduled注解实现定时任务
java·spring boot·后端
小王努力学编程36 分钟前
LangChain——AI应用开发框架
服务器·c++·人工智能·分布式·rpc·langchain·brpc