ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[原创] [e殿云]系列之Access财务记账管理系统

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2019-1-16 11:41 | 显示全部楼层 |阅读模式
本帖最后由 jsgj2023 于 2019-1-19 14:43 编辑

      最近因实际工作需要,写了个财务记账管理系统,本系统为本人结合实际工作需要编写,能极大地优化财税工作流程,提高工作效率,共分为2个部分:第一部分:表格交互;第二部分:数据库;表格交互部分共13个模块,一个模块占用一张表格,代码一般都在对应工作表下,函数过程和类模块需另建模块和类模块,13大模块分别为: 1.凭证录入 2.单笔凭证查询和删除 3.凭证批量查询 4.应收账款明细查询 5.应收账款汇总查询 6.应付账款明细查询 7.应付账款汇总查询 8.试算平衡 9.会计科目汇总表  10.成本利润核算 11.银行日记账 12.现金日记账 13.税收筹划; 数据库部分因涉及单位商业机密,仅分享代码及数据库结构,如有需要请自行按照本篇文章设计部署,在实施过程中如碰到问题或有建议请联系本人!












e殿云_记账管理系统_分享版.rar

172.09 KB, 下载次数: 1303

评分

13

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 11:52 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 jsgj2023 于 2019-1-17 10:24 编辑
  1. Sub 凭证录入()
复制代码
  1.     Dim arr, arr1, x As Integer, mydate As Date, hm As String, sr As String, SQL As String
  2.     Dim mydata As New data查询
  3.     With Sheets("凭证录入")
  4.                 mydate = .[C3]: hm = .[f3]
  5.                 If mydata.是否存在("和美贸易", "凭证号", hm) = True Or Len(Range("a5")) = 0 Then
  6.                     MsgBox "该凭证已存在或没有数据,请不要重复录入并添加数据!"
  7.                     Exit Sub
  8.                 Else
  9.                     r = .Cells(Rows.Count, 2).End(xlUp).Row - 2
  10.                     arr = .Range("a5:f" & r)
  11.                     'b = 借贷平衡检查(arr)
  12.                     If 借贷平衡检查(arr) Then
  13.                         For x = 1 To UBound(arr)
  14.                                 If Len(arr(x, 2)) Then
  15.                                     arr(x, 3) = IIf(IsEmpty(arr(x, 3)), 0, arr(x, 3))
  16.                                     arr(x, 4) = IIf(IsEmpty(arr(x, 4)), 0, arr(x, 4))
  17.                                     arr(x, 5) = IIf(IsEmpty(arr(x, 5)), 0, arr(x, 5))
  18.                                     arr(x, 6) = IIf(IsEmpty(arr(x, 6)), 0, arr(x, 6))
  19.                                     sr = "#" & mydate & "#" & ",'" & hm & "','" & arr(x, 1) & "','" & arr(x, 2) & "','"
  20.                                     sr = sr & arr(x, 3) & "','" & arr(x, 4) & "'," & Round(arr(x, 5), 2) & "," & arr(x, 6)
  21.                                     SQL = "Insert into 和美贸易 (记账日期, 凭证号, 摘要,总账科目,二级科目,三级科目,借方金额,贷方金额) VALUES(" & sr & ")"
  22.                                     mydata.执行sql命令 (SQL)
  23.                                 End If
  24.                         Next x
  25.                         Call 清空已录数据及凭证号
  26.                         MsgBox "成功录入数据库"
  27.                     Else
  28.                         MsgBox "借贷不平衡,请检查!"
  29.                         Exit Sub
  30.                     End If
  31.                 End If
  32.     End With
  33. End Sub

  34. Function 借贷平衡检查(arr) As Boolean
  35.      Dim a  As Single
  36.      Dim b As Single
  37.         a = Application.Sum(Application.Index(arr, , 5))
  38.         b = Application.Sum(Application.Index(arr, , 6))
  39.         If a <> b Then
  40.             借贷平衡检查 = False
  41.         Else
  42.             借贷平衡检查 = True
  43.         End If
  44. End Function
  45. Sub 清空已录数据及凭证号()
  46. Dim currentNum As Integer '当前凭证号
  47. 'Current certificate number 当前凭证号
  48.     With Sheets("凭证录入")
  49.         r = .Cells(Rows.Count, 1).End(xlUp).Row - 2 - 4
  50.         .Range("a5").Resize(r, 6).ClearContents
  51.         currentNum = .Range("f3")
  52.         currentNum = currentNum + 1
  53.         .Range("f3") = currentNum
  54.     End With
  55. End Sub
复制代码
本段代码放在模块[凭证录入]下

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 11:53 | 显示全部楼层
本帖最后由 jsgj2023 于 2019-1-17 10:25 编辑

1.凭证录入
凭证录入.gif

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 12:16 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 jsgj2023 于 2019-1-17 14:44 编辑
  1. Public recordColumnNo As Integer '记录列号
  2. '=================填表助手===============
  3. Option Compare Text
  4. Private Const LowBound = 1 '设定执行智能匹配时输入字符串的最小长度,以便减少无意义的匹配动作
  5. Private rDataS As Long
  6. Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  7. Call 输入
  8. End Sub

  9. Private Sub TextBox1_Change()
  10. txt = Me.TextBox1.Text
  11. End Sub

  12. Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  13.    Dim i As Integer
  14.     Select Case KeyCode
  15.     Case vbKeyE 'ctr+e切换输入状态
  16.         If Shift = 2 Then Call 填表助手.输入状态切换
  17.     Case vbKeyDelete '按delete键,则清空输入框及活动单元格
  18.         ActiveCell = ""
  19.         TextBox1.Text = ""
  20.         ListBox1.Clear
  21.         txt = ""
  22.     Case Else
  23.         Call SmartCompletion
  24.     End Select
  25. End Sub

  26. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  27.     If UserInput Then Exit Sub
  28.     With Target
  29. '        If .Count > 1 Or .Column <> 8 Or .Row < 2 Then ZoomOut: Exit Sub '调整数据输入列
  30.         If .Column <> 1 And .Column <> 2 And .Column <> 3 And .Column <> 4 Or .Row < 5 Or .Row > 12 Then ZoomOut: Exit Sub '会计科目
  31.         recordColumnNo = .Column '记录列号
  32.         If .Count > 1 And .Row < 5 Or .Row > 12 Then ZoomOut: Exit Sub
  33.          L = .Left
  34.          t = .Top
  35.          w = .Width
  36.          h = .Height + 2
  37.          s = .Font.Size - 1
  38.     End With
  39.      With Me.TextBox1
  40.         .Left = L
  41.         .Top = t
  42.         .Width = w
  43.         .Height = h
  44.         .Font.Size = s
  45.         .Visible = True
  46.         .Text = ""
  47.         .Activate
  48.     End With
  49.     With Me.ListBox1
  50.         .Left = L
  51.         .Width = w
  52.         .Top = t + h + 2
  53.         .Height = h * 10
  54.         .Font.Size = s
  55.         .Clear
  56.         .Visible = True
  57.     End With
  58. End Sub

  59. Private Function ZoomOut()
  60.     Application.ScreenUpdating = False
  61.      With Me.ListBox1
  62.         .Width = 0
  63.         .Height = 0
  64.         .Visible = False
  65.         .Clear
  66.     End With
  67.     With Me.TextBox1 '
  68.         .Width = 0
  69.         .Height = 0
  70.         .Visible = False
  71.         .Text = ""
  72.     End With
  73.     Application.ScreenUpdating = True
  74. End Function

  75. 'Public Dic As Object '三级科目
  76. 'Public dicAbstract As Object '摘要
  77. 'Public dicSumAccount As Object '总账科目
  78. 'Public dicSecondarySubject As Object '二级科目

  79. Private Sub SmartCompletion() '自动完成
  80.     Dim s As String
  81.     Dim strKey As Variant
  82.     Dim cnn As Object, SQL As String, arr, brr(), i&, j&
  83.     Dim DataSource(1 To 65536, 1 To 1)
  84.     Application.ScreenUpdating = False
  85.    Call 会计凭证.会计科目存为数组
  86.     If recordColumnNo = 1 Then '摘要
  87.         strKey = dicAbstract.keys
  88.         For x = 0 To dicAbstract.Count - 1
  89.             k = k + 1
  90.             DataSource(k, 1) = strKey(x)
  91.         Next
  92.     ElseIf recordColumnNo = 2 Then '总账科目
  93.         strKey = dicSumAccount.keys
  94.         For x = 0 To dicSumAccount.Count - 1
  95.             k = k + 1
  96.             DataSource(k, 1) = strKey(x)
  97.         Next
  98.     ElseIf recordColumnNo = 3 Then '二级科目
  99.         strKey = dicSecondarySubject.keys
  100.         For x = 0 To dicSecondarySubject.Count - 1
  101.             k = k + 1
  102.             DataSource(k, 1) = strKey(x)
  103.         Next
  104.     Else '三级科目
  105.         strKey = dic.keys
  106.         For x = 0 To dic.Count - 1
  107.         k = k + 1
  108.         DataSource(k, 1) = strKey(x)
  109.         Next
  110.     End If
  111. Application.ScreenUpdating = True
  112.     If Len(txt) < LowBound Then Exit Sub
  113.       s = "*" & UCase(TextBox1.Text) & "*"
  114.     With ListBox1
  115.         .Clear
  116.         rDataS = UBound(DataSource)
  117.         On Error Resume Next
  118.         For i = 1 To rDataS '
  119.             If DataSource(i, 1) Like s Then .AddItem DataSource(i, 1)
  120.         Next
  121.         If .ListCount = 0 Then
  122.             .Clear
  123.             .ListIndex = -1
  124.         Else
  125.             .ListIndex = 0
  126.         End If
  127.         .Height = ActiveCell.Height * IIf(.ListCount > 20, 20, 10)
  128.     End With
  129. End Sub

  130. Private Sub 输入()
  131.     If ListBox1.ListCount = 0 Then Exit Sub
  132.     ReDim Arraylist(0 To ListBox1.ListCount - 1, 0)
  133.     For i = 0 To ListBox1.ListCount - 1
  134.         If ListBox1.Selected(i) = True Then
  135.             n = n + 1
  136.             Arraylist(n - 1, 0) = ListBox1.List(i)
  137.         End If
  138.     Next
  139.     If n > 0 Then ActiveCell.Resize(n, 1) = Arraylist '如果有选中的项,则把它输入工作表中
  140.     ActiveCell.Offset(n).Select
  141. End Sub



复制代码
放在[凭证录入]工作表下,

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 13:40 | 显示全部楼层
类模块1下的代码:
类模块
  1. Function 筛选结果(sq As String)
  2. Dim conn As Object
  3. Dim rst As Object
  4. Set conn = CreateObject("adodb.connection")
  5. Set rst = CreateObject("ADODB.Recordset")

  6. conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "/Database/会计凭证.mdb"
  7. Set rst = conn.Execute(sq)
  8. 筛选结果 = rst.GetRows
  9. conn.Close
  10. Set conn = Nothing
  11. End Function

  12. Sub 执行sql命令(sq As String)
  13.   Dim conn As Object
  14. Dim rst As Object
  15. Set conn = CreateObject("adodb.connection")
  16. Set rst = CreateObject("ADODB.Recordset")
  17. conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "/Database/会计凭证.mdb"
  18. conn.Execute (sq)
  19. conn.Close
  20. Set conn = Nothing
  21. End Sub


  22. Function 是否存在(ku As String, zd As String, zh As String) 'ku指定查找的表,zd是判断的字段,zh 是值
  23.   Dim conn As Object
  24. Dim rst As Object
  25. Set conn = CreateObject("adodb.connection")
  26. Set rst = CreateObject("ADODB.Recordset")
  27. Dim SQL As String, arr
  28. conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "/Database/会计凭证.mdb"

  29. SQL = "Select * from " & ku & " where " & zd & "='" & zh & "'"
  30. rst.Open SQL, conn, 1, 1
  31. If rst.RecordCount = 0 Then
  32.    是否存在 = False
  33. Else
  34.    是否存在 = True
  35. End If
  36. conn.Close
  37. Set rst = Nothing
  38. Set conn = Nothing
  39. End Function

  40. Sub 执行筛选(sq, rg As String)
  41.      Dim conn As Object
  42.     Set conn = CreateObject("adodb.connection")
  43.     With ActiveSheet
  44.         conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "/Database/会计凭证.mdb"
  45.         .Range(rg).CopyFromRecordset conn.Execute(sq)
  46.         arrgetsource = conn.Execute(sq).GetRows
  47.     End With
  48.     conn.Close
  49.     Set conn = Nothing
  50. End Sub
复制代码
名称为[Data查询]

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 13:42 | 显示全部楼层
ThisWorkbook下的代码:
  1. Private Sub Workbook_Open()
  2. Call 初始化切换按键
  3. Call 辅助查询.获取当前日期最后一笔凭证号码
  4. MsgBox "智能提示功能已经启用。" & Chr(10) & Chr(10) & _
  5.         "您可以再次按 Ctrl+E 组合键来切换" & Chr(10) & _
  6.         "智能提示功能的启用或关闭状态。", vbInformation, "温馨提示:"
  7. End Sub

  8. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  9. Application.DisplayAlerts = False
  10. ThisWorkbook.Save
  11. Application.DisplayAlerts = True
  12. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 13:49 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 13:52 | 显示全部楼层
2.单笔凭证查询和删除
  1. Sub 查询下一张凭证()
  2.     Dim ControlClicks As Long
  3.     Dim currentNum As Long
  4.     Dim demandNum As String
  5.     Dim mydata As New data查询
  6.     Dim SQL As String
  7.     Dim num As String
  8.     With Me
  9.         .Range("a5").Resize(6, 6) = ""
  10.         currentNum = .[f3]
  11.         ControlClicks = ControlClicks + 1
  12.         demandNum = currentNum + ControlClicks
  13.         If mydata.是否存在("和美贸易", "凭证号", demandNum) = False Then
  14.             MsgBox "该凭证号不存在!"
  15.             Exit Sub
  16.         Else
  17.             SQL = "Select * from 和美贸易 Where  凭证号=" & "'" & demandNum & "'" & " "
  18.             sql2 = "select t1.摘要,t1.总账科目,t1.二级科目,t1.三级科目,t1.借方金额,t1.贷方金额 from (" & SQL & ") as t1 "
  19.             sql3 = "Select DISTINCT 记账日期 from 和美贸易 Where  凭证号=" & "'" & demandNum & "'" & " "
  20.             mydata.执行筛选 sql2, "a5"
  21.             mydata.执行筛选 sql3, "c3" '记账日期
  22.         End If
  23.         .[f3] = demandNum
  24.     End With
  25. End Sub

  26. Sub 查询上一张凭证()
  27.     Dim ControlClicks As Long
  28.     Dim currentNum As Long
  29.     Dim demandNum As String
  30.     Dim mydata As New data查询
  31.     Dim SQL As String
  32.     Dim num As String
  33.     With Me
  34.         .Range("a5").Resize(6, 6) = ""
  35.         currentNum = .[f3]
  36.         ControlClicks = ControlClicks - 1
  37.         demandNum = currentNum + ControlClicks
  38.         If mydata.是否存在("和美贸易", "凭证号", demandNum) = False Then
  39.             MsgBox "该凭证号不存在!"
  40.             Exit Sub
  41.         Else
  42.             SQL = "Select * from 和美贸易 Where  凭证号=" & "'" & demandNum & "'" & " "
  43.             sql2 = "select t1.摘要,t1.总账科目,t1.二级科目,t1.三级科目,t1.借方金额,t1.贷方金额 from (" & SQL & ") as t1 "
  44.             sql3 = "Select DISTINCT 记账日期 from 和美贸易 Where  凭证号=" & "'" & demandNum & "'" & " "
  45.             mydata.执行筛选 sql2, "a5"
  46.             mydata.执行筛选 sql3, "c3" '记账日期
  47.         End If
  48.         .[f3] = demandNum
  49.     End With
  50. End Sub

  51. Sub 按凭证号查询凭证()
  52. Dim mydata As New data查询
  53. Dim SQL As String
  54. Dim num As String
  55.     With Me
  56.         .Range("a5").Resize(6, 6) = ""
  57.         num = .[f3]
  58.         If mydata.是否存在("和美贸易", "凭证号", num) = False Then
  59.             MsgBox "该凭证号不存在!"
  60.             Exit Sub
  61.         Else
  62.             '& "','" & arr(x, 4) & "'"
  63.             SQL = "Select * from 和美贸易 Where  凭证号=" & "'" & num & "'" & " "
  64.             sql2 = "select t1.摘要,t1.总账科目,t1.二级科目,t1.三级科目,t1.借方金额,t1.贷方金额 from (" & SQL & ") as t1 "
  65.             sql3 = "Select DISTINCT 记账日期 from 和美贸易 Where  凭证号=" & "'" & num & "'" & " "
  66.             mydata.执行筛选 sql2, "a5"
  67.             mydata.执行筛选 sql3, "c3" '记账日期
  68.         End If
  69.     End With
  70. End Sub

  71. Sub 凭证修改()
  72.     Dim arr, arr1, x As Integer, mydate As Date, hm As String, sr As String, SQL As String
  73.     Dim mydata As New data查询
  74.     Call Me.按凭证号删除凭证
  75.     With Me
  76.         If Len(.Range("c5")) Then
  77.             mydate = .[C3]: hm = .[f3]
  78. '                If mydata.是否存在("和美贸易", "凭证号", hm) = True Then
  79. '                    MsgBox "该凭证号已存在!"
  80. '                    Exit Sub
  81. '                Else
  82.                     r = .Cells(Rows.Count, 2).End(xlUp).Row - 2
  83.                     arr = .Range("a5:f" & r)
  84.                     'b = 借贷平衡检查(arr)
  85.                     If 凭证录入.借贷平衡检查(arr) Then
  86.                         For x = 1 To UBound(arr)
  87.                                 If Len(arr(x, 2)) Then
  88.                                     arr(x, 3) = IIf(IsEmpty(arr(x, 3)), 0, arr(x, 3))
  89.                                     arr(x, 4) = IIf(IsEmpty(arr(x, 4)), 0, arr(x, 4))
  90.                                     arr(x, 5) = IIf(IsEmpty(arr(x, 5)), 0, arr(x, 5))
  91.                                     arr(x, 6) = IIf(IsEmpty(arr(x, 6)), 0, arr(x, 6))
  92.                                     sr = "#" & mydate & "#" & ",'" & hm & "','" & arr(x, 1) & "','" & arr(x, 2) & "','"
  93.                                     sr = sr & arr(x, 3) & "','" & arr(x, 4) & "'," & Round(arr(x, 5), 2) & "," & arr(x, 6)
  94.                                     SQL = "Insert into 和美贸易 (记账日期, 凭证号, 摘要,总账科目,二级科目,三级科目,借方金额,贷方金额) VALUES(" & sr & ")"
  95.                                     mydata.执行sql命令 (SQL)
  96.                                 End If
  97.                         Next x
  98.                         'Call 清空已录数据及凭证号
  99.                         MsgBox "成功修改数据并录入!"
  100.                     Else
  101.                         MsgBox "借贷不平衡,请检查!"
  102.                         Exit Sub
  103.                     End If
  104.                 'End If
  105.         Else
  106.             MsgBox "没有数据!"
  107.         End If
  108.     End With
  109. End Sub
  110. Sub 按凭证号删除凭证()
  111. Dim data As New data查询, SQL As String
  112. With Me
  113. If data.是否存在("和美贸易", "凭证号", .[f3]) = False Then
  114. MsgBox "此凭证号不存在!"
  115. Exit Sub
  116. Else
  117. SQL = "Delete from 和美贸易 where 凭证号='" & [f3] & "'"
  118. data.执行sql命令 SQL
  119. MsgBox "已删除凭证号为" & [f3] & "的凭证!"
  120. End If
  121. End With
  122. End Sub
复制代码
单笔查询修改.gif

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 13:59 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub 凭证批量查询()
  2. Dim mydata As New data查询
  3. Dim SQL As String
  4. Dim yearInt As Variant
  5. Dim monthInt As Variant
  6. Dim dataStr As Variant
  7. On Error GoTo ErrMsg
  8. With Me
  9.     .Range("a2:i65536") = ""
  10.     yearInt = "'" & .[O1] & "'"
  11.     monthInt = "'" & .[Q1] & "'"
  12.     dataStr = yearInt & "-" & monthInt
  13.     If .Range("N1") <> "年度" Then MsgBox "日期位置错误,请确保N列为年度字段,P列为月份字段!": Exit Sub
  14.     If Len(Range("O1")) = 0 Then MsgBox "请选择年度!": Exit Sub
  15.     If Len(Range("Q1")) <> 0 Then
  16.      SQL = "Select 记账日期,凭证号,摘要,总账科目,二级科目,三级科目,借方金额,贷方金额 from 和美贸易 where year(记账日期)= " & yearInt & " and month(记账日期)= " & monthInt & "order by 凭证号"
  17.     mydata.执行筛选 SQL, "a2"
  18.     Else
  19.      SQL = "Select 记账日期,凭证号,摘要,总账科目,二级科目,三级科目,借方金额,贷方金额 from 和美贸易 where year(记账日期)= " & yearInt & "order by 凭证号"
  20.     mydata.执行筛选 SQL, "a2"
  21.     End If
  22. End With
  23. Exit Sub
  24. ErrMsg:
  25.     MsgBox Err.Description, , "错误报告"
  26. End Sub

  27. Private Sub Worksheet_Change(ByVal Target As Range)
  28.     If Target.Column = 17 Or Target.Column = 15 And Target.Row = 1 Then
  29.         Call Me.凭证批量查询
  30.     End If
  31. End Sub

复制代码

3.凭证批量查询
凭证批量查询.gif

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-16 14:04 | 显示全部楼层
4.应收账款明细查询
  1. 工作表下代码:
  2. Sub 应收账款明细查询()
  3.     Dim mydata As New data查询
  4.     Dim SQL As String
  5.     Dim customerName As String
  6.     Application.ScreenUpdating = False
  7.     With Me
  8.         .Range("a2:h65536").ClearContents
  9.         customerName = "'" & .[k1] & "'"
  10.         
  11.         If Len(Range("Q1")) > 0 And Len(Range("K1")) > 0 Then
  12.             datewhere = "year(记账日期) & month(记账日期) <=" & [O1] & [Q1] & ""
  13.             SQL = "Select 记账日期,year(记账日期) as 年,month(记账日期) as 月,凭证号,摘要,借方金额,贷方金额 ,(借方金额-贷方金额) as 余额 from 和美贸易 where  ( " & datewhere & " ) and  总账科目='应收账款' and 二级科目=" & customerName & " order by 记账日期 "
  14.         ElseIf Len(Range("Q1")) = 0 And Len(Range("K1")) = 0 Then
  15.             datewhere = "year(记账日期)  <=" & [O1] & ""
  16.             SQL = "Select 记账日期,year(记账日期) as 年,month(记账日期) as 月,凭证号,摘要,借方金额,贷方金额 ,(借方金额-贷方金额) as 余额 from 和美贸易 where  ( " & datewhere & " ) and  总账科目='应收账款'  order by 记账日期 "
  17.         ElseIf Len(Range("Q1")) = 0 And Len(Range("K1")) > 0 Then
  18.             datewhere = "year(记账日期)  <=" & [O1] & ""
  19.             SQL = "Select 记账日期,year(记账日期) as 年,month(记账日期) as 月,凭证号,摘要,借方金额,贷方金额 ,(借方金额-贷方金额) as 余额 from 和美贸易 where  ( " & datewhere & " ) and  总账科目='应收账款'  and 二级科目=" & customerName & "  order by 记账日期 "
  20.         Else
  21.             MsgBox "您组合有误,请重新选择!"
  22.             Exit Sub
  23.         End If
  24.         mydata.执行筛选 SQL, "a2"
  25.         Dim 计算本月合计和本年累计 As New 金额本月合计与本年累计
  26.         Call 计算本月合计和本年累计.theMonthTotal_CurrentYearCumulative
  27.     End With
  28.     Application.ScreenUpdating = True
  29. End Sub

  30. Private Sub Worksheet_Change(ByVal Target As Range)
  31.     If Target.Column = 17 Or Target.Column = 11 And Target.Row = 1 Then
  32.         Call Me.应收账款明细查询
  33.     End If
  34. End Sub

  35. '( " & datewhere & " ) and
  36. 'and 摘要  not like '结转上年余额'
  37. 'Dim totalAmount As New 金额合计
  38. 'Call totalAmount.应收明细查询合计

  39. 类模块下代码:
  40. Function theMonthTotal_CurrentYearCumulative() '本月合计 本年累计 the month total   current year cumulative
  41.     'With Sheets("应收账款明细查询")
  42.     With ActiveSheet
  43.         Dim d  As Object
  44.         Dim dRow As Object
  45.         Dim dRowTotal As Object
  46.         Dim arr As Variant
  47.         Dim r&, x&, y&, z&
  48.         Set d = CreateObject("scripting.dictionary") '金额
  49.         Set dRow = CreateObject("scripting.dictionary") '本月小计行号
  50.         Set dRowTotal = CreateObject("scripting.dictionary") '本年累计行号
  51.         r = .Cells(Rows.Count, 1).End(xlUp).Row
  52.         arr = .Range("a1:f" & r)
  53.         For x = UBound(arr) To 3 Step -1
  54.             If Month(arr(x, 1)) <> Month(arr(x - 1, 1)) Then
  55.                 .Rows(x).Insert
  56.                 .Cells(x, 5) = "本年累计"
  57.                 .Rows(x).Insert
  58.                 .Cells(x, 5) = "本月小计"
  59.             End If
  60.         Next
  61.         ' 末尾写入本年累计和本月小计 字段
  62.         r = .Cells(.Rows.Count, 3).End(xlUp).Row
  63.         yy = .Cells(r, 3)
  64.         i = r
  65.         Do While .Cells(i, 3) = yy
  66.             i = i - 1
  67.         Loop
  68.         .Cells(r + 1, 5) = "本月小计"
  69.         .Cells(r + 2, 5) = "本年累计"
  70.         Dim arrInserted As Variant
  71.          r = .Cells(.Rows.Count, 5).End(xlUp).Row
  72.         '重新获取表格数据并写入数组
  73.          arr1 = .Range("a1:h" & r)
  74.          For x = 2 To UBound(arr1)
  75.             If arr1(x, 5) = "本月小计" Then dRow(x) = ""
  76.             If arr1(x, 5) = "本年累计" Then
  77.                 n = n + 1
  78.                 dRowTotal(x) = n
  79.             End If
  80.             If Len(arr1(x, 3)) Then
  81.                  If Not d.exists(arr1(x, 3)) Then
  82.                    d(arr1(x, 3)) = Array(arr1(x, 6), arr1(x, 7), arr1(x, 8))
  83.                Else
  84.                    k = d(arr1(x, 3))
  85.                    k(0) = k(0) + arr1(x, 6)
  86.                    k(1) = k(1) + arr1(x, 7)
  87.                    k(2) = k(2) + arr1(x, 8)
  88.                    d(arr1(x, 3)) = k
  89.                End If
  90.              End If
  91.          Next
  92.          
  93.         Dim strdItems As Variant
  94.         Dim strdRowKeys As Variant
  95.          strdItems = d.items
  96.          strdRowKeys = dRow.keys
  97.          For z = 0 To d.Count - 1
  98.             .Cells(strdRowKeys(z), 6).Resize(1, 3) = Application.Transpose(Application.Transpose(strdItems(z))) '本月合计
  99.          Next
  100.   '求本年累计
  101.         Dim sMonth As Variant
  102.         strdtotalitems = dRowTotal.items
  103.         strdtotalKeys = dRowTotal.keys
  104.         For i = 0 To dRowTotal.Count - 1
  105.             '借方金额
  106.             s = strdtotalitems(i)
  107.             sMonth = Application.Index(strdItems, , 1)
  108.             .Cells(strdtotalKeys(i), 6) = MonthlySum(s1, sMonth)
  109.             '贷方金额
  110.             sMonth = Application.Index(strdItems, , 2)
  111.             .Cells(strdtotalKeys(i), 7) = MonthlySum(s2, sMonth)
  112.             '余额
  113.             sMonth = Application.Index(strdItems, , 3)
  114.             .Cells(strdtotalKeys(i), 8) = MonthlySum(s, sMonth)
  115.         Next
  116.     End With
  117. End Function

  118. Function MonthlySum(strMonth, arr)
  119.     For x = 1 To strMonth
  120.         sSum = sSum + arr(x, 1)
  121.     Next
  122.     MonthlySum = sSum
  123. End Function
复制代码

应收明细查询.gif
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-3 18:19 , Processed in 0.048435 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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