Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: 新会甜橙

[已解决]求助,修改宏

[复制链接]
发表于 2012-12-3 13:25 | 显示全部楼层
  1. Sub 数据3()
  2.     Dim arr
  3.     Dim iRow&, i&, wbzb$
  4.    
  5.     '读取总表源数据
  6.     wbzb = "总表"
  7.     ThisWorkbook.Activate
  8.     With Worksheets(wbzb)
  9.         If Len(.[a3]) = 0 Then Exit Sub
  10.         '高级筛选,去除重复值
  11.         .Range("a3").CurrentRegion.AdvancedFilter xlFilterInPlace, , , True
  12.         iRow = .Range("c" & Rows.Count).End(xlUp).Row
  13.         If iRow <= 3 Then Exit Sub  '最后一行数据行低于第3行位置就退出
  14.         arr = .Range("a3:i" & iRow)
  15.     End With
  16.    
  17.     'arr2数组存储数据
  18.     Dim dic As Object, arr2(), k&, j&
  19.     '数据列对应关系,arrZos源列号,arrMpos目标列号
  20.     Dim arrZPos, arrMPos, arrTemp
  21.     arrZPos = Array(4, 5, 6, 7, 8, 9)
  22.     arrMPos = Array(1, 2, 3, 4, 5, 10)
  23.    
  24.     '以支行网点名字存入字典
  25.     Set dic = CreateObject("Scripting.dictionary")
  26.     For i = 2 To UBound(arr)
  27.         If Not dic.exists(arr(i, 3)) Then
  28.             ReDim arr2(1 To 10, 1 To 1)
  29.             For j = LBound(arrZPos) To UBound(arrZPos)
  30.                 arr2(arrMPos(j), 1) = arr(i, arrZPos(j))
  31.             Next
  32.             dic(arr(i, 3)) = Array(1, arr2) '0为存入个数,1为数组
  33.         Else
  34.             arrTemp = dic(arr(i, 3))
  35.             k = arrTemp(0) + 1
  36.             arr2 = arrTemp(1)
  37.             ReDim Preserve arr2(1 To 10, 1 To k)
  38.             For j = LBound(arrZPos) To UBound(arrZPos)
  39.                 arr2(arrMPos(j), k) = arr(i, arrZPos(j))
  40.             Next
  41.             dic(arr(i, 3)) = Array(k, arr2)
  42.         End If
  43.     Next

  44.     On Error Resume Next
  45.     Dim wb As Workbook
  46.     Dim wbname$, Slash$
  47.     wbname = "明细.xls"
  48.     Slash = Application.PathSeparator

  49.     Dim secAutomation As MsoAutomationSecurity
  50.     Set wb = Workbooks(wbname)
  51.     If Err.Number <> 0 Then
  52.         Err.Clear
  53.         MsgBox ThisWorkbook.Path & Slash & wb
  54.         '防止打开时运行宏
  55.         secAutomation = Application.AutomationSecurity
  56.         Application.AutomationSecurity = msoAutomationSecurityForceDisable
  57.         Set wb = Workbooks.Open(ThisWorkbook.Path & Slash & wbname)
  58.         If Err.Number <> 0 Then
  59.             MsgBox "打开 " & wb & " 出错"
  60.             Err.Clear
  61.             Exit Sub
  62.         End If
  63.     End If
  64.    
  65.     wb.Activate

  66.     Dim arrKey, wbZong$, keyitem, endrow2&, endrow&
  67.     For Each keyitem In dic.keys
  68.         With Worksheets(keyitem)
  69.             If Err.Number = 0 Then
  70.                 .Range("d:e").NumberFormatLocal = "@"
  71.                 .Range("f:f").NumberFormatLocal = "G/通用格式"
  72.                  endrow = .Range("c" & Rows.Count).End(xlUp).Row = 3
  73.                 If endrow > 3 Then
  74.                     .Range("a4:j" & endrow).ClearContents
  75.                     .Range("a4:j" & endrow).Borders.LineStyle = xlNone
  76.                 End If
  77.                 endrow = 4
  78.                 arr2 = WorksheetFunction.Transpose(dic(keyitem)(1))
  79.                 .Range("a" & endrow).Resize(UBound(arr2), 10) = arr2
  80.                 endrow2 = .Range("c" & Rows.Count).End(xlUp).Row
  81.                 .Range("f" & endrow).FormulaR1C1 = "=IF(COUNTIF(R4C[5]:RC11,RC[-1]&""*"")=1,""√"","""")"
  82.                 .Range("f" & endrow & ":f" & endrow2).FillDown
  83.                 With .Range("a" & 3 & ":j" & endrow2).Borders
  84.                         .LineStyle = xlContinuous
  85.                 End With
  86.                 .Range("a:j").Columns.AutoFit
  87.             End If
  88.             Err.Clear
  89.         End With
  90.     Next
  91.     Application.AutomationSecurity = msoAutomationSecurityByUI
  92. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2012-12-3 20:12 | 显示全部楼层
hwc2ycy 发表于 2012-12-3 13:25

老师,还有要求是拆分的各工作表无需调整格式,因为已调整好各表的打印格式了,只需填入内容就行了,望解决,多谢!
回复

使用道具 举报

发表于 2012-12-3 20:23 | 显示全部楼层    本楼为最佳答案   
  1. Sub 数据3()
  2.     Dim arr
  3.     Dim iRow&, i&, wbzb$
  4.    
  5.     '读取总表源数据
  6.     wbzb = "总表"
  7.     ThisWorkbook.Activate
  8.     With Worksheets(wbzb)
  9.         If Len(.[a3]) = 0 Then Exit Sub
  10.         '高级筛选,去除重复值
  11.         .Range("a3").CurrentRegion.AdvancedFilter xlFilterInPlace, , , True
  12.         iRow = .Range("c" & Rows.Count).End(xlUp).Row
  13.         If iRow <= 3 Then Exit Sub  '最后一行数据行低于第3行位置就退出
  14.         arr = .Range("a3:i" & iRow)
  15.     End With
  16.    
  17.     'arr2数组存储数据
  18.     Dim dic As Object, arr2(), k&, j&
  19.     '数据列对应关系,arrZos源列号,arrMpos目标列号
  20.     Dim arrZPos, arrMPos, arrTemp
  21.     arrZPos = Array(4, 5, 6, 7, 8, 9)
  22.     arrMPos = Array(1, 2, 3, 4, 5, 10)
  23.    
  24.     '以支行网点名字存入字典
  25.     Set dic = CreateObject("Scripting.dictionary")
  26.     For i = 2 To UBound(arr)
  27.         If Not dic.exists(arr(i, 3)) Then
  28.             ReDim arr2(1 To 10, 1 To 1)
  29.             For j = LBound(arrZPos) To UBound(arrZPos)
  30.                 arr2(arrMPos(j), 1) = arr(i, arrZPos(j))
  31.             Next
  32.             dic(arr(i, 3)) = Array(1, arr2) '0为存入个数,1为数组
  33.         Else
  34.             arrTemp = dic(arr(i, 3))
  35.             k = arrTemp(0) + 1
  36.             arr2 = arrTemp(1)
  37.             ReDim Preserve arr2(1 To 10, 1 To k)
  38.             For j = LBound(arrZPos) To UBound(arrZPos)
  39.                 arr2(arrMPos(j), k) = arr(i, arrZPos(j))
  40.             Next
  41.             dic(arr(i, 3)) = Array(k, arr2)
  42.         End If
  43.     Next

  44.     On Error Resume Next
  45.     Dim wb As Workbook
  46.     Dim wbname$, Slash$
  47.     wbname = "明细.xls"
  48.     Slash = Application.PathSeparator

  49.     Dim secAutomation As MsoAutomationSecurity
  50.     Set wb = Workbooks(wbname)
  51.     If Err.Number <> 0 Then
  52.         Err.Clear
  53.         MsgBox ThisWorkbook.Path & Slash & wb
  54.         '防止打开时运行宏
  55.         secAutomation = Application.AutomationSecurity
  56.         Application.AutomationSecurity = msoAutomationSecurityForceDisable
  57.         Set wb = Workbooks.Open(ThisWorkbook.Path & Slash & wbname)
  58.         If Err.Number <> 0 Then
  59.             MsgBox "打开 " & wb & " 出错"
  60.             Err.Clear
  61.             Exit Sub
  62.         End If
  63.     End If
  64.    
  65.     wb.Activate

  66.     Dim arrKey, wbZong$, keyitem, endrow2&, endrow&
  67.     For Each keyitem In dic.keys
  68.         With Worksheets(keyitem)
  69.             If Err.Number = 0 Then
  70.                 .Range("d:e").NumberFormatLocal = "@"
  71.                 .Range("f:f").NumberFormatLocal = "G/通用格式"
  72.                  endrow = .Range("c" & Rows.Count).End(xlUp).Row = 3
  73.                 If endrow > 3 Then
  74.                     .Range("a4:j" & endrow).ClearContents
  75.                     .Range("a4:j" & endrow).Borders.LineStyle = xlNone
  76.                 End If
  77.                 endrow = 4
  78.                 arr2 = WorksheetFunction.Transpose(dic(keyitem)(1))
  79.                 .Range("a" & endrow).Resize(UBound(arr2), 10) = arr2
  80.                 endrow2 = .Range("c" & Rows.Count).End(xlUp).Row
  81.                 .Range("f" & endrow).FormulaR1C1 = "=IF(COUNTIF(R4C[5]:RC11,RC[-1]&""*"")=1,""√"","""")"
  82.                 .Range("f" & endrow & ":f" & endrow2).FillDown
  83.                 End If
  84.             Err.Clear
  85.         End With
  86.     Next
  87.     Application.AutomationSecurity = msoAutomationSecurityByUI
  88. End Sub
复制代码

评分

参与人数 1 +1 收起 理由
新会甜橙 + 1 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2012-12-3 22:01 | 显示全部楼层
hwc2ycy 发表于 2012-12-3 20:23

感谢老师,太完美了
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 14:14 , Processed in 0.387162 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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