【代码】往来账清账勾对,字典实例详解,把多个单元格区域组合装入字典

文摘   教育   2024-10-17 23:57   江苏  

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

快速浏览

实用案例

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

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

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

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

|印章使用登记系统|

收费使用项目

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

内容提要

  • 往来账清账勾对|完整代码
1、在工作表“Sheet1”里,命令按钮点击事件,调用勾对过程:
Private Sub CmdCheck_Click()    Call billCheckEnd Sub
2、在myModule里,billCheck过程,进行往来勾对:
第一部分,把数据装入字典:
Sub billCheck()    Dim ws As Worksheet, rng As Range, row As Range, Area As Range    Dim cArea As Range, dArea As Range, cRow As Range, dRow As Range    Dim lRow As Long    Dim lCol As Integer    Dim cTotal As Double, dTotal As Double    Dim dic As Object, dkey    Dim bgColor1 As Double, bgColor2 As Double    bgColor1 = RGB(173, 255, 47)    bgColor2 = RGB(255, 235, 205)    Set ws = ThisWorkbook.Sheets("Sheet1")    Set dic = CreateObject("Scripting.Dictionary")        '//把相同供应商业务编号所在行组合成一个Range,装入字典    With ws        lRow = .UsedRange.Rows.Count        lCol = 6        For i = 2 To lRow            If .Cells(i, 2) <> "" Then                .Cells(i, lCol) = ""                dkey = .Cells(i, 2) & "|" & .Cells(i, 3)                If Not dic.exists(dkey) Then                    Set rng = .Cells(i, 1).Resize(1, lCol)                Else                    Set rng = dic(dkey)                    Set rng = Union(rng, .Cells(i, 1).Resize(1, lCol))                End If                Set dic(dkey) = rng            End If        Next    End With
第二部分,循环字典的key,取出item进行勾对处理:
    '//循环字典的每一个key,把item取出来进行勾对处理    For Each dkey In dic.keys        Set rng = dic(dkey)        rng.Interior.Color = xlNone        dTotal = 0: cTotal = 0                '//先汇总借、贷方金额        For Each Area In rng.Areas            For Each row In Area.Rows                dTotal = dTotal + row.Cells(1, 4)                cTotal = cTotal + row.Cells(1, 5)            Next        Next        If dTotal = cTotal Then                        '//如果借贷方汇总金额相同,两清,勾对所有            For Each Area In rng.Areas                For Each row In Area.Rows                    row.Cells(1, lCol) = "y"                    row.Interior.Color = bgColor1                Next            Next        Else                        '//如果借、贷方汇总金额不等,再进行明细勾对            For Each dArea In rng.Areas                For Each dRow In dArea.Rows                    dTotal = dRow.Cells(1, 4)                    If dTotal <> 0 Then  '//排除借方为0的记录                        For Each cArea In rng.Areas                            For Each cRow In cArea.Rows                                cTotal = cRow.Cells(1, 5)                                If dTotal = cTotal And cRow.Cells(1, lCol) <> "y" Then                                                                        '//如果借贷相等,且贷方没有被勾对过                                    dRow.Cells(1, lCol) = "y"                                    dRow.Interior.Color = bgColor2                                    cRow.Cells(1, lCol) = "y"                                    cRow.Interior.Color = bgColor2                                                                        '//勾对一条记录就退出循环,防止把其他相同金额也勾对                                    GoTo nextRow                                End If                            Next                        Next                    End IfnextRow:                Next            Next        End If    Next    MsgBox "Done!"End Sub


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

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

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

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

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

  • 如需案例文件,请按当天另一篇文章末尾案例文件分享说明操作!

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