Excel精英培训网

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

[已解决]数据条件求和后合并

[复制链接]
发表于 2014-2-9 17:49 | 显示全部楼层
看看行不行
  1. Sub BH()
  2. Dim s As String, i As Long, cnt As Integer, m, n, k
  3. Range("c4:c" & Range("c65536").End(3).Row).UnMerge
  4. For i = 4 To Range("d65536").End(3).Row
  5.    For cnt = 1 To Range("d65536").End(3).Row
  6.    n = Cells(i, "d").Resize(cnt)
  7.      m = WorksheetFunction.Sum(n)
  8.      If m > 1 Then
  9.      k = k + 1
  10.       Application.DisplayAlerts = False
  11.    Cells(i, "c").Resize(cnt).Merge
  12.    Cells(i, "c").Value = Cells(i, "g") & Format(k, "000")
  13.   Application.DisplayAlerts = True
  14.   i = i + cnt - 1
  15.      GoTo 100
  16.      End If
  17.      Next
  18. i = i + cnt - 1
  19. 100: Next
  20. Cells(cnt - 1, "c") = Cells(cnt - 1, "g") & Format(k + 1, "000")
  21. End Sub
复制代码

条件求和后合并单元格(编号2).rar

21.29 KB, 下载次数: 4

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2014-2-9 19:00 | 显示全部楼层    本楼为最佳答案   
  1. Dim Arr, i&, j&, aa, h, ks, js, n&, k
  2. Sub lqxs()
  3. Dim d, t
  4. Set d = CreateObject("Scripting.Dictionary")
  5. Sheet1.Activate
  6. With [d:d]
  7.     .ClearContents
  8.     .UnMerge
  9.     .Interior.ColorIndex = xlNone
  10. End With
  11. Arr = [e3].CurrentRegion
  12. [d3].Resize(UBound(Arr), 1).Borders.LineStyle = 1
  13. For i = 2 To UBound(Arr)
  14.     d(Arr(i, 4)) = d(Arr(i, 4)) & i & ","
  15. Next
  16. k = d.keys: t = d.items
  17. For i = 0 To UBound(k)
  18.     t(i) = Left(t(i), Len(t(i)) - 1): n = 0
  19.     If InStr(t(i), ",") Then
  20.         aa = Split(t(i), ",")
  21.         ks = aa(0) + 2: h = 0
  22.         For j = 0 To UBound(aa)
  23.             h = h + Arr(aa(j), 1)
  24.             If Abs(h - 1) < 0.1 Then
  25.                 js = aa(j) + 2
  26.                 Call yy
  27.                 If j + 1 <= UBound(aa) Then ks = aa(j + 1) + 2
  28.             ElseIf h > 1 Then
  29.                 js = aa(j - 1) + 2
  30.                 h = h - Arr(aa(j), 1)
  31.                 Call yy
  32.                 If j + 1 <= UBound(aa) Then ks = aa(j) + 2: h = Arr(aa(j), 1)
  33.             Else
  34.                 GoTo 100
  35.             End If
  36. 100:
  37.         Next
  38.     Else
  39.     End If
  40. Next
  41. If h <> 0 Then js = UBound(Arr) + 2: Call yy
  42. End Sub
  43. Sub yy()
  44. Dim ys
  45. If h > 1 Then
  46.     ys = 6
  47. ElseIf h < 1 Then
  48.     ys = 4
  49. End If
  50. With Cells(ks, 4).Resize(js - ks + 1, 1)
  51.     .Merge
  52.     n = n + 1
  53.     .Value = Arr(ks - 2, 4) & Format(n, "00")
  54.     .Interior.ColorIndex = ys: h = 0
  55. End With
  56. End Sub
复制代码
   看看这个吧

条件求和后合并单元格最终.rar

23.59 KB, 下载次数: 12

回复

使用道具 举报

 楼主| 发表于 2014-2-9 19:10 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-13 08:04 , Processed in 0.168098 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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