【代码】数据匹配:不用字典,如何查找日期相同、数值相近的数据?

文摘   教育   2024-10-04 19:42   江苏  

点【关于本公众号】了解一下,欢迎关注谢谢!

快速浏览

实用案例

|日期控件||简单的收发存||收费管理系(Access改进版)|

|电子发票管理助手||电子发票登记系统(Access版)|

|文件合并||表格拆分||审计凭证抽查底稿|

|中医诊所收费系统(Excel版)||中医诊所收费系统(Access版)||银行对账单自动勾对|

|印章使用登记系统|

收费使用项目

|财务管理系统||工资薪金和年终奖个税筹划||新税法下工资表模版|

内容提要

  • 查找日期相同、数值相近的数据|完整代码
1、在工作表“Sheet1”里,命令按钮点击事件,调用查询过程:
Private Sub CmdQuery_Click()    matchType = ""    Call myQueryEnd Sub
Private Sub CmdUpClose_Click() matchType = "大于" Call myQueryEnd Sub
Private Sub CmdDownClose_Click() matchType = "小于" Call myQueryEnd Sub
2、在myModule模块里,myQuery过程,根据matchType的值,查询不同类型的数据:
Sub myQuery()    Dim ws1 As Worksheet, ws2 As Worksheet, rng1 As Range, rng2 As Range    Dim cell1 As Range, cell2 As Range    Dim date1 As Date, date2 As Date, value1 As Double, value2 As Double    Dim i As Integer, lRow  As Integer, lCol As Integer    Dim arr(), IsValid As Boolean    Dim dic As Object    Set ws1 = ThisWorkbook.Sheets("Sheet1")    Set ws2 = ThisWorkbook.Sheets("Sheet2")        '//原数据区域    With ws1        lRow = .Cells(.Rows.Count, 2).End(xlUp).Row        lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column        Set rng1 = .Range(.Cells(3, 2), .Cells(lRow, lCol))    End With        '//查找区域    With ws2        lRow = .Cells(.Rows.Count, 5).End(xlUp).Row        lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column        Set rng2 = .Range(.Cells(3, 5), .Cells(lRow, lCol))    End With        '//循环原数据区域每一行    For Each cell1 In rng1.Columns(1).Cells        date1 = cell1.Value        value1 = cell1.Offset(0, 1).Value        cell1.Offset(, 2) = ""                '//循环查找区域每一行        For Each cell2 In rng2.Columns(1).Cells            date2 = cell2.Value            value2 = cell2.Offset(, 1).Value            IsValid = False                        '//如果日期相同            If date1 = date2 Then                                '//与原数相比,如果两个数相差1%                If Abs((value1 - value2) / value1) <= 0.01 Then                                        '//根据matchType判断当前记录是否符合要求                    If matchType = "大于" Then                        If value1 < value2 Then                            IsValid = True                        End If                    ElseIf matchType = "小于" Then                        If value1 >= value2 Then                            IsValid = True                        End If                    Else                        IsValid = True                    End If                    If IsValid Then                                                '//找到第一个符合条件的值,填入目标单元格                        If Val(cell1.Offset(, 2)) = 0 Then                            cell1.Offset(, 2) = value2                                                        '//如果已取到数,取与原数相差更小的                        ElseIf Abs(cell1.Offset(, 2) - value1) > Abs(value2 - value1) Then                            cell1.Offset(, 2) = value2                        End If                    End If                End If            End If        Next    NextEnd Sub
3、在myModule模块里,sLookUp自定义函数,查找日期相同、数值相近的数
Function sLookUp(ref1, ref2 As Double, _        dRatio As Double, rng As Range)    Dim cell As Range    Dim preValue As Double, lastValue As Double, currValue As Double    For Each cell In rng.Columns(1).Cells        If cell.Value = ref1 Then            currValue = cell.Offset(0, rng.Columns.Count - 1)            If ref2 >= currValue And (ref2 - currValue) / ref2 <= dRatio Then                If preValue < currValue Then                    lastValue = currValue                    preValue = currValue                End If            End If        End If    Next    sLookUp = lastValueEnd Function

~~~~~~End~~~~~~

安利小店
安利的牙膏非常不错,用了以后就不想再用其他的了;洗洁精洗衣液也是日常必备,用过都说好!

合谷医疗
合谷医疗专攻各种疑难杂症,尤其擅长抑郁症焦虑失眠儿童神经发育异常多动症自闭孤独症腰颈椎疾病治疗,可谓神乎其技!体验过的直呼早点来就好了

喜欢就点个、点在看留言评论、分享一下呗!感谢支持!

  • Excel问题,请在文章下面留言讨论!或者加入我的付费交流群提问

VBA编程实战
Excel应用案例、Excel VBA、公式函数使用技巧分享,思路解读...... 这里有鲜活案例、实用的技巧......
 最新文章