用于合并同一行中多个列中的单元格的宏 [英] Macro for merging cells in multiple columns across the same row

查看:110
本文介绍了用于合并同一行中多个列中的单元格的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您愿意,我正在创建一种数据库,并且我在一行中有多个列需要合并,并且想知道是否可以使用宏来做到这一点.

I am creating a kind of database if you will and i have multiple columns in the one row that need to be merged and am wondering if it's possible to do this with a macro.

要使其变得更加困难,合并的单元格数量不是恒定的. IE浏览器:

To make it even more difficult the amount of cells to merge isn't constant. IE:

  1 A   B   C   MARY JO         50  main office     admin

  2 A   B   C   MARY JO         50  main office     admin

  3 A   B   C   MARY JO         50  main office     admin

  1 A   B   C   JOHN DOE        60  OTHER office    driver

  2 A   B   C   JOHN DOE        60  OTHER office    driver

合并第1-3行和第4-5行是理想的结果.没有发布图片的能力很难解释,但是A和Z列开始要取消合并,因为每一行的信息都显示在这些列中.

Merge rows 1-3 and 4-5 is the desired outcome. Hard to explain without ability to post picture but column A and Z onwards want UNMERGED as each individual row has information displayed in these columns.

现在看起来像什么:

WHAT IT LOOKS LIKE NOW:

期望的最终结果:

DESIRED FINAL OUTCOME:

推荐答案

尝试如下操作:

Private Sub MergeTheseCellsToMakeSomethingLikeADatabase()
    'Crucial Line Below
    Application.DisplayAlerts = False
    Dim i%
    With ThisWorkbook.Sheets("Sheet1")
       For i = .UsedRange.Rows.Count to 2 Step -1
          If .Cells(i,4).Value = .Cells(i-1,4).Value Then
             .Range(.Cells(i,2),.Cells(i-1,2)).Merge
             .Range(.Cells(i,3),.Cells(i-1,3)).Merge
             .... 
             'Do this for all rows that are relevant or just have another for loop to cycle
             'Through the rows that you want merged      
          End If
       Next
    End With
End Sub

这篇关于用于合并同一行中多个列中的单元格的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆