Excel精英培训网

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

[已解决]vb合并单元格问题

[复制链接]
发表于 2017-7-7 16:29 | 显示全部楼层 |阅读模式

合并单元格

合并单元格

如图,表中第三和第四行相同内容需合并单元格,以下代码出错,请问大神们.merge这句哪里错了?该如何修改?谢谢
Sub hbrglx()
Application.DisplayAlerts = False
Dim i As Integer
For i = 2 To 7
If Cells(4, i) = Cells(5, i) Then
Range("cells(4,i):cells(5,i)").Merge
End If
Next
Application.DisplayAlerts = True
End Sub


最佳答案
2017-7-7 16:37
本帖最后由 chart888 于 2017-7-7 16:41 编辑
  1. Sub hbrglx()
  2. Application.DisplayAlerts = False
  3. Dim i As Integer
  4. For i = 2 To 7
  5.     If Cells(4, i) = Cells(5, i) Then
  6.         Cells(4, i).Resize(2, 1).Merge
  7.     End If
  8. Next
  9. Application.DisplayAlerts = True
  10. End Sub
复制代码


或者用下面的

  1. Sub hbrglx1()
  2. Application.DisplayAlerts = False
  3. Dim i As Integer
  4. For i = 2 To 7
  5.     If Cells(4, i) = Cells(5, i) Then
  6.         Range(Cells(4, i), Cells(5, i)).Merge
  7.     End If
  8. Next
  9. Application.DisplayAlerts = True
  10. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-7-7 16:37 | 显示全部楼层    本楼为最佳答案   
本帖最后由 chart888 于 2017-7-7 16:41 编辑
  1. Sub hbrglx()
  2. Application.DisplayAlerts = False
  3. Dim i As Integer
  4. For i = 2 To 7
  5.     If Cells(4, i) = Cells(5, i) Then
  6.         Cells(4, i).Resize(2, 1).Merge
  7.     End If
  8. Next
  9. Application.DisplayAlerts = True
  10. End Sub
复制代码


或者用下面的

  1. Sub hbrglx1()
  2. Application.DisplayAlerts = False
  3. Dim i As Integer
  4. For i = 2 To 7
  5.     If Cells(4, i) = Cells(5, i) Then
  6.         Range(Cells(4, i), Cells(5, i)).Merge
  7.     End If
  8. Next
  9. Application.DisplayAlerts = True
  10. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2017-7-7 17:20 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 23:07 , Processed in 0.424123 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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