单独合并选择中的变量行
我有一个 Excel 2007 表格,如下所示:
/| A | B | C | D
-+---------+----------+----------+----------+
1| Item1 | Info a | 1200 | sum(C1:C2)
2| | | 2130 |
3| Item2 | Info b | 2100 | sum(C3:C7)
5| | | 11 |
6| | | 12121 |
7| | | 123 |
8| Item3 | Info c | 213 | sum(C8:C10)
9| | | 233 |
10| | | 111 |
我希望做的是,每当我选择整个表格(上例中的 A1:C10
)并按
,宏代码将自动将空白单元格与其上方包含文本的单元格合并,例如A1
到A2
; A3
到 A7
等等。 B
列也是如此。对于D
列,合并后,它还会对C
列中的所有项目进行求和。我可以手动进行合并和求和,但这会花费我相当长的时间,所以我一直在研究宏以使生活更轻松。
我想强调的是,每个项目上要合并的行数是可变的(Item 1
只有 2 行 - A1
和 A2
, Item 2
有 4 个,依此类推。)
这可以在 Excel VBA 中实现吗?非常感谢任何帮助和评论。
I have an Excel 2007 table which looks like this:
/| A | B | C | D
-+---------+----------+----------+----------+
1| Item1 | Info a | 1200 | sum(C1:C2)
2| | | 2130 |
3| Item2 | Info b | 2100 | sum(C3:C7)
5| | | 11 |
6| | | 12121 |
7| | | 123 |
8| Item3 | Info c | 213 | sum(C8:C10)
9| | | 233 |
10| | | 111 |
What I hope to do is that whenever I select the entire table (A1:C10
for the above example) and press <Ctrl> + <M>
, the macro code will automatically merge the blank cells with the cell above them that contains text e.g. A1
to A2
; A3
to A7
and so forth. The same goes for column B
. For column D
, after merging, it would also sum up all the items in column C
. I could do the merging and summation manually, however it would take me quite a while so I've been looking into macros to make life easier.
I would like to emphasize that the number of rows to merge on each item is variable (Item 1
has only 2 rows - A1
and A2
, Item 2
has 4, and so on.)
Is this possible to do in Excel VBA? Any help and comments are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有大量行,请避免循环单元格本身,因为这非常慢。首先将单元格值复制到 Variant 数组中。
从 Excel、工具/宏菜单、选项设置快捷键
If you have a large number of rows, avoid looping through the cells themselves, as this is quite slow. Instaed copy the cells values to a Variant array first.
Set Quick Key from Excel, Tools/Macros menu, Options