ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 920|回复: 0

[求助] 拉伸试验查询表中的VBA编程中出现的问题求助

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-9-4 09:45 | 显示全部楼层 |阅读模式

现因工作需要,使用EXCEL 2007的VBA 编程文件编制了拉伸试验查询表,现发现部分标准的第一行查询数据无,其余均正确;现苦思不得其解,请各位前辈给予指导,十分感谢!



Sub BiaoZhun()
    On Error Resume Next
    Dim x, Arr1, D1, L1
    Set D1 = CreateObject("scripting.dictionary")
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    D1.comparemode = vbTextCompare
    With Sheets("拉伸试验原始数据")
        Arr1 = .Range("a1").CurrentRegion
        For x = 2 To UBound(Arr1)
            D1(Arr1(x, 1)) = ""
        Next x
    End With
    L1 = Join(Application.Transpose(Application.Transpose(D1.KEYS)), ",")
    Range("B1:b3").Validation.Delete
    Range("b2:b3").ClearContents
    Range("B1").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=L1
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

Sub PaiHao()
    On Error Resume Next
    Application.EnableEvents = False
    If Range("b1") <> "" Then
        Application.ScreenUpdating = False
        Dim x, Arr1, D1, Up, Ed
        Set D1 = CreateObject("scripting.dictionary")
        D1.comparemode = vbTextCompare
        With Sheets("拉伸试验原始数据")
            Arr1 = .Range("a1").CurrentRegion
            Up = .Range("a:a").Find(Range("b1"), , , xlWhole).Row
            Ed = .Range("a:a").Find(Range("b1"), , , xlWhole, , xlPrevious).Row
            For x = Up To Ed
                If Arr1(x, 1) = Range("b1") Then
                    D1(Arr1(x, 2)) = ""
                End If
            Next x
        End With
        Range("B2:b3").Validation.Delete
        Range("b3").ClearContents
        If D1.Count > 0 Then
            L1 = Join(Application.Transpose(Application.Transpose(D1.KEYS)), ",")
            Range("B2").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=L1
        End If
        Application.ScreenUpdating = True
    End If
    Application.EnableEvents = True
End Sub
Sub GuiGe()
    On Error Resume Next
    Application.EnableEvents = False
    If Range("b1") <> "" And Range("b2") <> "" Then
        Application.ScreenUpdating = False
        Dim x, Arr1, D1, Up, Ed
        Set D1 = CreateObject("scripting.dictionary")
        D1.comparemode = vbTextCompare
        With Sheets("拉伸试验原始数据")
            Arr1 = .Range("a1").CurrentRegion
            Up = .Range("a:a").Find(Range("b1"), , , xlWhole).Row
            Ed = .Range("a:a").Find(Range("b1"), , , xlWhole, , xlPrevious).Row
            For x = Up To Ed
                If Arr1(x, 1) = Range("b1") And Arr1(x, 2) = Range("b2") And Arr1(x, 3) <> "" Then
                    D1(Arr1(x, 3)) = ""
                End If
            Next x
        End With
        Range("B3").Validation.Delete
        If D1.Count > 0 Then
            Range("b3") = "选择规格"
            L1 = Join(Application.Transpose(Application.Transpose(D1.KEYS)), ",")
            Range("B3").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=L1
        Else
            Range("b3") = "-"
        End If
        Application.ScreenUpdating = True
    End If
    Application.EnableEvents = True
End Sub

Sub List1()
    On Error Resume Next
    Application.EnableEvents = False
    With Sheets("拉伸试验原始数据")
        Application.ScreenUpdating = False
        Dim x, Y, K, Arr1, Arr2(), D1, Up, Ed
        Arr1 = .Range("a1").CurrentRegion
        ReDim Arr2(1 To UBound(Arr1), 1 To 2)
        Up = .Range("a:a").Find(Range("b1"), , , xlWhole).Row
        Ed = .Range("a:a").Find(Range("b1"), , , xlWhole, , xlPrevious).Row
        For x = Up To Ed
            If Arr1(x, 2) = Range("b2") Then
                For Y = 4 To UBound(Arr1, 2)
                    If Arr1(x, Y) <> "" Then
                        K = K + 1
                        Arr2(K, 1) = Arr1(1, Y)
                        Arr2(K, 2) = Arr1(x, Y)
                    End If
                Next Y
                Exit For
            End If
        Next x
        Range("a6").Resize(10000, 4).ClearContents
        Range("a6").Resize(K, 2) = Arr2
        Application.ScreenUpdating = True
    End With
    Application.EnableEvents = True
End Sub
Sub list2()
    On Error Resume Next
    Application.EnableEvents = False
    With Sheets("拉伸试验原始数据")
        Application.ScreenUpdating = False
        Dim x, Y, K, Arr1, Arr2(), Up, Ed, Up2, Ed2
        Arr1 = .Range("a1").CurrentRegion
        ReDim Arr2(1 To UBound(Arr1), 1 To 2)
        Up = .Range("a:a").Find(Range("b1"), , , xlWhole).Row
        Ed = .Range("a:a").Find(Range("b1"), , , xlWhole, , xlPrevious).Row
        Up2 = .Range(.Cells(Up, 2), .Cells(Ed, 2)).Find(Range("b2"), , , xlWhole).Row
        Ed2 = .Range(.Cells(Up, 2), .Cells(Ed, 2)).Find(Range("b2"), , , xlWhole, , xlPrevious).Row
        For x = Up2 To Ed2
            If Arr1(x, 3) = Range("b3") Then
                For Y = 4 To UBound(Arr1, 2)
                    If Arr1(x, Y) <> "" Then
                        K = K + 1
                        Arr2(K, 1) = Arr1(1, Y)
                        Arr2(K, 2) = Arr1(x, Y)
                    End If
                Next Y
                Exit For
            End If
        Next x
        Range("a6").Resize(10000, 4).ClearContents
        Range("a6").Resize(K, 2) = Arr2
    End With
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub



Private Sub Worksheet_Activate()
    Call BiaoZhun
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    If Target.Address = "$B$1" Then
        Call PaiHao
        Call GuiGe
    End If
    If Target.Address = "$B$2" Then
        Call PaiHao
        Call GuiGe
        Range("a6").Resize(10000, 4).ClearContents
        If Range("b3") = "-" Then
            Call List1
        End If
    End If
    If Target.Address = "$B$3" Then
        Range("a6").Resize(10000, 4).ClearContents
        If Range("b3") <> "选择规格" And Range("b3") <> "-" And Range("b3") <> "" Then
            Call list2
        End If
    End If
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    Application.ScreenUpdating = False
    If Target.Address = "$B$1" Then
        Call PaiHao
        Call GuiGe
    End If
    If Target.Address = "$B$2" Then
'        Call PaiHao
        Call GuiGe
    End If
'    If Target.Address = "$B$3" Then
'        Call GuiGe
'    End If
    Application.ScreenUpdating = True
End Sub

Sub ttt()
    Application.EnableEvents = True

End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-13 13:42 , Processed in 0.018505 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表