Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2160|回复: 7

[已解决]分享并求肋

[复制链接]
发表于 2017-6-6 11:59 | 显示全部楼层 |阅读模式
老师们中午好!我最近在学编VBA代码!但运行时出了点问题,请各位老师斧正!修改后的代码在宏中,仍未最终成功。原来的代码在sheet2中,运行中出现的问题和要达到的目的在sheet1中。谢谢!
最佳答案
2017-6-6 14:54
  1. Sub aaa()
  2. Dim Arr, myPath$, myName$, Arr1, i&, j&, x$, y$
  3. Dim crr2(1 To 10000, 1 To 3)
  4. Dim d, d1, bt, k, t, k1, aa, s$
  5. Dim bb, m&, col&
  6. Set d = CreateObject("Scripting.Dictionary")
  7. Set d1 = CreateObject("Scripting.Dictionary")
  8. Application.DisplayAlerts = False
  9. Application.ScreenUpdating = False

  10. Sheet1.Activate
  11. bt = Array("工作时间", "加班时间")
  12. [a3:e5000].Clear
  13. [f1:bz5002].Clear
  14. Cells.Font.Size = 11
  15. myPath = ThisWorkbook.Path & ""
  16. myName = Dir(myPath & "*.xls")
  17. Do While myName <> ""
  18.     If InStr(myName, "汇总") = 0 Then
  19.        With GetObject(myPath & myName)
  20.            Arr1 = .Sheets(1).[a1].CurrentRegion
  21.            For i = 3 To UBound(Arr1)
  22.                x = Arr1(i, 2)
  23.                y = Split(myName, ".")(0)
  24.                If d.exists(x) = False Then
  25.                     Set d(x) = CreateObject("Scripting.Dictionary")
  26.                     nn = nn + 1
  27.                     crr2(nn, 1) = Arr1(i, 3)
  28.                     crr2(nn, 2) = Arr1(i, 4)
  29.                     crr2(nn, 3) = Arr1(i, 5)
  30.                End If
  31.                d(x)(y) = d(x)(y) & Arr1(i, 6) & "|" & Arr1(i, 7)
  32.                d1(y) = ""
  33.            Next
  34.            .Close False
  35.        End With
  36.     End If
  37. myName = Dir
  38. Loop
  39. k = d.keys: t = d.items: k1 = d1.keys
  40. For i = 0 To UBound(k1) + 1
  41.     If i <> UBound(k1) + 1 Then s = k1(i) Else s = "合计"
  42.         With Cells(1, 2 * i + 6).Resize(1, 2)
  43.              .Value = s
  44.              .Merge
  45.              .HorizontalAlignment = -4108
  46.              .VerticalAlignment = -4108
  47.         End With
  48.         Cells(2, 2 * i + 6).Resize(1, 2) = bt
  49. Next
  50. col = 2 * d1.Count + 6
  51. [b3].Resize(d.Count) = Application.Transpose(k)
  52. crr = [b3].Resize(d.Count)
  53. [c3].Resize(nn, 3) = crr2
  54. [a3] = 1: [a4] = 2: [a3:a4].AutoFill [a3].Resize(d.Count)
  55. m = d.Count + 3
  56. Cells(m, 2) = "合计"
  57. For i = 0 To UBound(k)
  58.     For j = 0 To UBound(k1)
  59.         If d(k(i)).exists(k1(j)) Then
  60.             bb = d(k(i))(k1(j))
  61.             aa = Split(bb, "|")
  62.             Cells(i + 3, 2 * j + 6) = Val(aa(0))
  63.             Cells(i + 3, 2 * j + 7) = Val(aa(1))
  64.             Cells(i + 3, col) = Cells(i + 3, col) + Val(aa(0))
  65.             Cells(i + 3, col + 1) = Cells(i + 3, col + 1) + Val(aa(1))
  66.         End If
  67.     Next
  68. Next
  69. For i2 = 6 To col + 1
  70. Cells(m, i2) = WorksheetFunction.Sum(Range(Cells(3, i2).Address, Cells(m - 1, i2).Address))
  71. Next

  72. [a1].CurrentRegion.Borders.LineStyle = 1
  73. Application.DisplayAlerts = True
  74. Application.ScreenUpdating = True
  75. End Sub
复制代码

偷懒,在你代码基础上稍微加了点
实现了你的要求

合并汇总(新).rar

143.58 KB, 下载次数: 6

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-6-6 13:32 | 显示全部楼层
  1. Sub aaa()
  2.     Dim Arr, myPath$, myName$, Arr1, i&, j&, x$, y$
  3.     Dim d, d1, bt, k, t, k1, aa, s$
  4.     Dim bb, m&, col&
  5.     Set d = CreateObject("Scripting.Dictionary")
  6.     Set d1 = CreateObject("Scripting.Dictionary")
  7.     ActiveWindow.DisplayZeros = False
  8.     Application.DisplayAlerts = False
  9.     Application.ScreenUpdating = False
  10.     Sheet1.Activate
  11.     bt = Array("工作时间", "加班时间")
  12.     [a3:e5000].Clear
  13.     [f1:bz5002].Clear
  14.     Cells.Font.Size = 11
  15.     myPath = ThisWorkbook.Path & ""
  16.     myName = Dir(myPath & "*.xls")
  17.     Do While myName <> ""
  18.        If InStr(myName, "汇总") = 0 Then
  19.        With GetObject(myPath & myName)
  20.            Arr1 = .Sheets(1).[a1].CurrentRegion
  21.            For i = 3 To UBound(Arr1)
  22.                x = Arr1(i, 2): y = Split(myName, ".")(0)
  23.                If d.exists(x) = False Then Set d(x) = CreateObject("Scripting.Dictionary")
  24.                d(x)(y) = d(x)(y) & Arr1(i, 6) & "|" & Arr1(i, 6)
  25.                d1(y) = ""
  26.            Next
  27.            .Close False
  28.        End With
  29.        End If
  30.        myName = Dir
  31.     Loop
  32.     k = d.keys: t = d.items: k1 = d1.keys
  33.     For i = 0 To UBound(k1) + 1
  34.         If i <> UBound(k1) + 1 Then s = k1(i) Else s = "合计"
  35.         With Cells(1, 2 * i + 6).Resize(1, 2)
  36.              .Value = s
  37.              .Merge
  38.              .HorizontalAlignment = -4108
  39.              .VerticalAlignment = -4108
  40.         End With
  41.         Cells(2, 2 * i + 6).Resize(1, 2) = bt
  42.     Next
  43.     col = 2 * d1.Count + 6
  44.     [b3].Resize(d.Count) = Application.Transpose(k)
  45.     [a3] = 1: [a4] = 2: [a3:a4].AutoFill [a3].Resize(d.Count)
  46.     m = d.Count + 3
  47.     Cells(m, 2) = "合计"
  48.     For i = 0 To UBound(k)
  49.         For j = 0 To UBound(k1)
  50.             If d(k(i)).exists(k1(j)) Then
  51.             bb = d(k(i))(k1(j))
  52.             aa = Split(bb, "|")
  53.             Cells(i + 3, 2 * j + 6) = Val(aa(0))
  54.             Cells(i + 3, 2 * j + 7) = Val(aa(1))
  55.             Cells(i + 3, col) = Cells(i + 3, col) + Val(aa(0))
  56.             Cells(i + 3, col + 1) = Cells(i + 3, col + 1) + Val(aa(1))
  57.             End If
  58.         Next
  59.     Next
  60.     Cells(m, 6) = WorksheetFunction.Sum(Range(Cells(3, 6).Address, Cells(m - 1, 6).Address))
  61.     Cells(m, 6).AutoFill Cells(m, 6).Resize(1, col - 4)
  62.     [a1].CurrentRegion.Borders.LineStyle = 1
  63.     Application.DisplayAlerts = True
  64.     Application.ScreenUpdating = True
  65. End Sub
复制代码



改了一句

合并汇总(新).zip

144.02 KB, 下载次数: 4

回复

使用道具 举报

 楼主| 发表于 2017-6-6 13:41 | 显示全部楼层
改了一句,已经很厉害了,我怎么改都没有改出效果来!,老师,能否将C-E列数据也读过来,加班时间也不对,现加班时间中读入的是工作时间。谢谢老师!
回复

使用道具 举报

发表于 2017-6-6 14:28 | 显示全部楼层
  1. d(x)(y) = d(x)(y) & Arr1(i, 6) & "|" & Arr1(i, 7)
复制代码

加班时间的问题,改这一条就行了
回复

使用道具 举报

 楼主| 发表于 2017-6-6 14:54 | 显示全部楼层
谢谢老师,可能中午没有睡觉,脑子是浑的!能否将C-E列数据也读过来?我现在脑子是一片浆糊啊!
回复

使用道具 举报

发表于 2017-6-6 14:54 | 显示全部楼层    本楼为最佳答案   
  1. Sub aaa()
  2. Dim Arr, myPath$, myName$, Arr1, i&, j&, x$, y$
  3. Dim crr2(1 To 10000, 1 To 3)
  4. Dim d, d1, bt, k, t, k1, aa, s$
  5. Dim bb, m&, col&
  6. Set d = CreateObject("Scripting.Dictionary")
  7. Set d1 = CreateObject("Scripting.Dictionary")
  8. Application.DisplayAlerts = False
  9. Application.ScreenUpdating = False

  10. Sheet1.Activate
  11. bt = Array("工作时间", "加班时间")
  12. [a3:e5000].Clear
  13. [f1:bz5002].Clear
  14. Cells.Font.Size = 11
  15. myPath = ThisWorkbook.Path & ""
  16. myName = Dir(myPath & "*.xls")
  17. Do While myName <> ""
  18.     If InStr(myName, "汇总") = 0 Then
  19.        With GetObject(myPath & myName)
  20.            Arr1 = .Sheets(1).[a1].CurrentRegion
  21.            For i = 3 To UBound(Arr1)
  22.                x = Arr1(i, 2)
  23.                y = Split(myName, ".")(0)
  24.                If d.exists(x) = False Then
  25.                     Set d(x) = CreateObject("Scripting.Dictionary")
  26.                     nn = nn + 1
  27.                     crr2(nn, 1) = Arr1(i, 3)
  28.                     crr2(nn, 2) = Arr1(i, 4)
  29.                     crr2(nn, 3) = Arr1(i, 5)
  30.                End If
  31.                d(x)(y) = d(x)(y) & Arr1(i, 6) & "|" & Arr1(i, 7)
  32.                d1(y) = ""
  33.            Next
  34.            .Close False
  35.        End With
  36.     End If
  37. myName = Dir
  38. Loop
  39. k = d.keys: t = d.items: k1 = d1.keys
  40. For i = 0 To UBound(k1) + 1
  41.     If i <> UBound(k1) + 1 Then s = k1(i) Else s = "合计"
  42.         With Cells(1, 2 * i + 6).Resize(1, 2)
  43.              .Value = s
  44.              .Merge
  45.              .HorizontalAlignment = -4108
  46.              .VerticalAlignment = -4108
  47.         End With
  48.         Cells(2, 2 * i + 6).Resize(1, 2) = bt
  49. Next
  50. col = 2 * d1.Count + 6
  51. [b3].Resize(d.Count) = Application.Transpose(k)
  52. crr = [b3].Resize(d.Count)
  53. [c3].Resize(nn, 3) = crr2
  54. [a3] = 1: [a4] = 2: [a3:a4].AutoFill [a3].Resize(d.Count)
  55. m = d.Count + 3
  56. Cells(m, 2) = "合计"
  57. For i = 0 To UBound(k)
  58.     For j = 0 To UBound(k1)
  59.         If d(k(i)).exists(k1(j)) Then
  60.             bb = d(k(i))(k1(j))
  61.             aa = Split(bb, "|")
  62.             Cells(i + 3, 2 * j + 6) = Val(aa(0))
  63.             Cells(i + 3, 2 * j + 7) = Val(aa(1))
  64.             Cells(i + 3, col) = Cells(i + 3, col) + Val(aa(0))
  65.             Cells(i + 3, col + 1) = Cells(i + 3, col + 1) + Val(aa(1))
  66.         End If
  67.     Next
  68. Next
  69. For i2 = 6 To col + 1
  70. Cells(m, i2) = WorksheetFunction.Sum(Range(Cells(3, i2).Address, Cells(m - 1, i2).Address))
  71. Next

  72. [a1].CurrentRegion.Borders.LineStyle = 1
  73. Application.DisplayAlerts = True
  74. Application.ScreenUpdating = True
  75. End Sub
复制代码

偷懒,在你代码基础上稍微加了点
实现了你的要求

合并汇总(新).zip

101.08 KB, 下载次数: 10

回复

使用道具 举报

 楼主| 发表于 2017-6-6 15:00 | 显示全部楼层
我就想中午能解决它,没睡觉,丢三拉四的。初学代码编写,真的很吃力!非常感谢老师的指导!
回复

使用道具 举报

发表于 2017-6-6 15:06 | 显示全部楼层
sunche 发表于 2017-6-6 15:00
我就想中午能解决它,没睡觉,丢三拉四的。初学代码编写,真的很吃力!非常感谢老师的指导!

可以的话
给个最佳
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-24 07:52 , Processed in 0.326050 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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