Excel精英培训网

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

[已解决]横向合并同类项

[复制链接]
发表于 2014-6-15 16:21 | 显示全部楼层 |阅读模式
本帖最后由 张雄友 于 2014-6-16 18:34 编辑

横向合并同类项。
最佳答案
2014-6-15 21:03
Sub TEST3()
    Application.DisplayAlerts = False
    ARR = Selection
    Set D = CreateObject("scripting.dictionary")
    For I = 1 To UBound(ARR, 2)
        If ARR(1, I) = "" Then Exit Sub
        If Not D.exists(ARR(1, I)) Then
            D.Add ARR(1, I), 1
        Else
            D(ARR(1, I)) = D(ARR(1, I)) + 1
        End If
    Next
    S = D.ITEMS
    L = Selection.Column - 1
    R = Selection.Row
    For I = 0 To UBound(S)
        If S(I) > 1 Then
            With Range(Cells(R, L + 1), Cells(R, L + S(I)))
                .Merge
                .HorizontalAlignment = xlCenter
                .Borders.LineStyle = xlContinuous
            End With
        Else
            With Cells(R, L + 1)
                .HorizontalAlignment = xlCenter
                .Borders.LineStyle = xlContinuous
            End With
        End If
        L = L + S(I)
    Next
    Application.DisplayAlerts = True
End Sub

横向合并同类项.rar

9.38 KB, 下载次数: 13

发表于 2014-6-15 18:38 | 显示全部楼层
横向合并同类项.rar (8.7 KB, 下载次数: 14)
回复

使用道具 举报

 楼主| 发表于 2014-6-15 18:45 | 显示全部楼层
zjdh 发表于 2014-6-15 18:38

直接指定区域太死板,用selection 属性你看看对不对??
  1. Sub TEST2()
  2.     Application.DisplayAlerts = False
  3.     arr = Selection
  4.     Set D = CreateObject("scripting.dictionary")
  5.     For I = 1 To UBound(arr, 2)
  6.         If Not D.exists(arr(1, I)) Then
  7.             D.Add arr(1, I), 1
  8.         Else
  9.             D(arr(1, I)) = D(arr(1, I)) + 1
  10.         End If
  11.     Next
  12.     S = D.ITEMS
  13.     L = Selection.Column - 1
  14.     For I = 0 To UBound(S)
  15.         If S(I) > 1 Then
  16.             With Range(Cells(Selection.Row, L + 1), Cells(Selection.Row, L + S(I)))
  17.                 .Merge
  18.                 .HorizontalAlignment = xlCenter
  19.                 .Borders.LineStyle = xlContinuous
  20.             End With
  21.         Else
  22.             With Cells(Selection.Row, L + 1)
  23.                 .HorizontalAlignment = xlCenter
  24.                 .Borders.LineStyle = xlContinuous
  25.             End With

  26.         End If
  27.         L = L + S(I)
  28.     Next
  29.     Application.DisplayAlerts = True
  30. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-6-15 18:47 | 显示全部楼层
zjdh 发表于 2014-6-15 18:38

不对的,因为只能执行一次,点多几下就出错了。
回复

使用道具 举报

发表于 2014-6-15 19:17 | 显示全部楼层
简单一点的,呵呵

横向合并同类项.rar

9.53 KB, 下载次数: 13

评分

参与人数 1 +6 收起 理由
张雄友 + 6 很给力!

查看全部评分

回复

使用道具 举报

发表于 2014-6-15 21:03 | 显示全部楼层    本楼为最佳答案   
Sub TEST3()
    Application.DisplayAlerts = False
    ARR = Selection
    Set D = CreateObject("scripting.dictionary")
    For I = 1 To UBound(ARR, 2)
        If ARR(1, I) = "" Then Exit Sub
        If Not D.exists(ARR(1, I)) Then
            D.Add ARR(1, I), 1
        Else
            D(ARR(1, I)) = D(ARR(1, I)) + 1
        End If
    Next
    S = D.ITEMS
    L = Selection.Column - 1
    R = Selection.Row
    For I = 0 To UBound(S)
        If S(I) > 1 Then
            With Range(Cells(R, L + 1), Cells(R, L + S(I)))
                .Merge
                .HorizontalAlignment = xlCenter
                .Borders.LineStyle = xlContinuous
            End With
        Else
            With Cells(R, L + 1)
                .HorizontalAlignment = xlCenter
                .Borders.LineStyle = xlContinuous
            End With
        End If
        L = L + S(I)
    Next
    Application.DisplayAlerts = True
End Sub
回复

使用道具 举报

 楼主| 发表于 2014-6-15 21:08 | 显示全部楼层
zjdh 发表于 2014-6-15 21:03
Sub TEST3()
    Application.DisplayAlerts = False
    ARR = Selection

如果是相反呢?就是反过来,怎么复原??
回复

使用道具 举报

发表于 2014-6-16 14:34 | 显示全部楼层
还有问题吗?

评分

参与人数 1 +2 收起 理由
张雄友 + 2 如果是相反呢?就是反过来,怎么复原??

查看全部评分

回复

使用道具 举报

发表于 2014-6-16 19:03 | 显示全部楼层
本帖最后由 zjdh 于 2014-6-16 19:05 编辑

Sub TEST4()
    Application.DisplayAlerts = False
    Selection.UnMerge
    ARR = Selection
    R = Selection.Row
    S = 0
    For I = UBound(ARR, 2) To 1 Step -1
        T = I + Selection.Column - 1
        If ARR(1, I) = "" Then
            S = S + 1
        Else
            Range(Cells(R, T), Cells(R, T + S)) = ARR(1, I)
            S = 0
        End If
    Next
End Sub

评分

参与人数 1 +6 收起 理由
张雄友 + 6 很给力!

查看全部评分

回复

使用道具 举报

发表于 2014-7-4 16:41 | 显示全部楼层
做得越多越好啊
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 06:41 , Processed in 0.240426 second(s), 19 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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