Excel精英培训网

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

[已解决]VBA代码修改

[复制链接]
发表于 2012-11-9 14:28 | 显示全部楼层 |阅读模式
题目要求:每隔一行插入两行空行
问题症状:代码只对前三行进行了插入,4行以后都没插入,如何对代码进行修改,问题出在什么地方?
Sub t2()
Dim x As Integer
For x = 2 To Range("a65536").End(xlUp).Row Step 3
Rows(x).Insert
Rows(x + 1).Insert
Next
End Sub
最佳答案
2012-11-9 14:49
  1. Sub t2()

  2.     Dim x As Integer
  3.     Dim rg As Range
  4.    
  5.     Application.ScreenUpdating = False
  6.     x = Range("a65536").End(xlUp).Row
  7.     On Error Resume Next
  8.     Set rg = Range("a1:a" & x).SpecialCells(xlCellTypeBlanks)
  9.     If Not rg Is Nothing Then
  10.         Range("a1:a" & x).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  11.     End If
  12.    
  13.     For x = Range("a65536").End(xlUp).Row To 2 Step -1
  14.         Rows(x).Resize(2).Insert
  15.     Next
  16.     Application.ScreenUpdating = True
  17. End Sub
复制代码
改一下,先清除原有空行再加。

第七讲(1-7题).rar

5.9 KB, 下载次数: 11

发表于 2012-11-9 14:36 | 显示全部楼层
回复

使用道具 举报

发表于 2012-11-9 14:41 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-11-9 14:52 编辑
  1. Sub t2()
  2.     Dim x As Integer
  3.     Application.ScreenUpdating = False
  4.     For x = Range("a65536").End(xlUp).Row To 2 Step -1
  5.         Rows(x).Resize(2).Insert
  6.     Next
  7.     Application.ScreenUpdating = True
  8. End Sub
复制代码
回复

使用道具 举报

发表于 2012-11-9 14:49 | 显示全部楼层    本楼为最佳答案   
  1. Sub t2()

  2.     Dim x As Integer
  3.     Dim rg As Range
  4.    
  5.     Application.ScreenUpdating = False
  6.     x = Range("a65536").End(xlUp).Row
  7.     On Error Resume Next
  8.     Set rg = Range("a1:a" & x).SpecialCells(xlCellTypeBlanks)
  9.     If Not rg Is Nothing Then
  10.         Range("a1:a" & x).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  11.     End If
  12.    
  13.     For x = Range("a65536").End(xlUp).Row To 2 Step -1
  14.         Rows(x).Resize(2).Insert
  15.     Next
  16.     Application.ScreenUpdating = True
  17. End Sub
复制代码
改一下,先清除原有空行再加。
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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