Excel精英培训网

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

[已解决]用VBA进行指定行的单元格合并

[复制链接]
发表于 2013-2-22 09:27 | 显示全部楼层 |阅读模式
要求在“原数据”工作表中。
谢谢!

问题2013.rar (5.83 KB, 下载次数: 31)
发表于 2013-2-22 09:50 | 显示全部楼层
回复

使用道具 举报

发表于 2013-2-22 10:12 | 显示全部楼层    本楼为最佳答案   
  1. Sub 合并数据()
  2.     Application.ScreenUpdating = False
  3.     Application.DisplayAlerts = False
  4.     Sheets("原数据").Columns("A:I").Copy
  5.     With Sheets("结果")
  6.         .Cells.Clear
  7.         Sheets("原数据").Columns("A:I").Copy
  8.         .Columns("A:A").Insert Shift:=xlToRight
  9.         Application.CutCopyMode = False
  10.         For I = 1 To .Range("I65536").End(xlUp).Row
  11.             Select Case .Range("I" & I)
  12.             Case "标题"
  13.                 .Range(.Cells(I, 1), .Cells(I, 7)).Merge
  14.                 .Range(.Cells(I, 1), .Cells(I, 7)).HorizontalAlignment = xlCenter
  15.             Case 2
  16.                 .Range(.Cells(I, 1), .Cells(I, 7)).Merge
  17.                 .Range(.Cells(I, 1), .Cells(I, 7)).HorizontalAlignment = xlLeft
  18.             Case 1
  19.                 .Range(.Cells(I, 1), .Cells(I, 2)).Merge
  20.                 .Range(.Cells(I, 1), .Cells(I, 2)).HorizontalAlignment = xlLeft
  21.                 .Range(.Cells(I, 3), .Cells(I, 4)).Merge
  22.                 .Range(.Cells(I, 3), .Cells(I, 4)).HorizontalAlignment = xlLeft
  23.                 .Range(.Cells(I, 5), .Cells(I, 7)).Merge
  24.                 .Range(.Cells(I, 5), .Cells(I, 7)).HorizontalAlignment = xlLeft
  25.             End Select
  26.         Next I
  27.         .Columns("H:I").Clear
  28.         .Rows("1:1").Delete
  29.         Sheets("样表").Columns("A:G").Copy
  30.         .Columns("A:G").PasteSpecial Paste:=xlPasteColumnWidths
  31.         Application.CutCopyMode = False
  32.         .Rows("1:" & .Range("A65536").End(xlUp).Row).RowHeight = 18
  33.     End With
  34.     Application.ScreenUpdating = True
  35.     Application.DisplayAlerts = True
  36. End Sub
复制代码
回复

使用道具 举报

发表于 2013-7-9 06:35 | 显示全部楼层
不明白解决了什么总是?
回复

使用道具 举报

发表于 2013-7-9 06:35 | 显示全部楼层
不明白解决了什么问题?
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-6 03:41 , Processed in 0.270606 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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