Excel精英培训网

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

[已解决]请帮忙修改一下代码,使之更简捷更方便

[复制链接]
发表于 2012-1-2 19:23 | 显示全部楼层 |阅读模式
以下我本人录制的一段宏,要达到的目的是插入行,同时复制上面行的公式,现只能实现的每点击一次“插入行”(指定了宏按钮)可以插入固定的行数(5行),但当要插入的行数较多时,就比较麻烦,现希望能做到每点击“插入行”就出现一个对话框,提示插入多少行,然后输入要插入的行数,这样可以一步到位,想请高手帮忙,并还请高手能不能使代码尽量简化,先谢了!


Sub Macro3()
'
' Macro3 Macro
' 宏由 yangkaifu 录制,时间: 2011-12-31
'
'
    ActiveSheet.Unprotect
    Rows("7:11").Select
    Selection.Insert Shift:=xlDown
    Range("B6:AD6").Select
    Selection.AutoFill Destination:=Range("B6:AD11"), Type:=xlFillDefault
    Range("B6:AD11").Select
    Range("F13").Select
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
        :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
End Sub

最佳答案
2012-1-2 20:34
ykfexcel 发表于 2012-1-2 20:15
感谢sunjing-zxl ,学习了,不过我还希望不需要提示输入行号,就直接从7行(或某个其他固定行)开始插入并复 ...
  1. Sub 插入复制()
  2. Dim Ro As Long, n As Long
  3. Ro = 7
  4. ActiveSheet.Unprotect
  5. n = InputBox("输入行数", "请输入需要插入行的行数")
  6. Rows(Ro & ":" & Ro + n - 1).Insert
  7. Rows(Ro - 1).Copy Rows(Ro & ":" & Ro + n - 1)
  8. ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
  9. False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
  10. AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
  11. :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
  12. AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
  13. AllowUsingPivotTables:=True
  14. End Sub
复制代码

发表于 2012-1-2 19:45 | 显示全部楼层
本帖最后由 sunjing-zxl 于 2012-1-2 19:46 编辑
  1. Sub 插入复制()
  2.     Dim Ro As Long, n As Long
  3.     Ro = InputBox("输入行号", "请输入需要插入行的行号")
  4.     n = InputBox("输入行数", "请输入需要插入行的行数")
  5.     Rows(Ro & ":" & Ro + n - 1).Insert
  6.     Rows(Ro - 1).Copy Rows(Ro & ":" & Ro + n - 1)
  7. End Sub
复制代码


回复

使用道具 举报

 楼主| 发表于 2012-1-2 20:15 | 显示全部楼层
感谢sunjing-zxl ,学习了,不过我还希望不需要提示输入行号,就直接从7行(或某个其他固定行)开始插入并复制而且还要求同时对工作表实现实时保护,烦请sunjing-zxl 能不能再指一二,不胜感谢!
附件如下

插入行代码.rar

11.15 KB, 下载次数: 12

回复

使用道具 举报

发表于 2012-1-2 20:34 | 显示全部楼层    本楼为最佳答案   
ykfexcel 发表于 2012-1-2 20:15
感谢sunjing-zxl ,学习了,不过我还希望不需要提示输入行号,就直接从7行(或某个其他固定行)开始插入并复 ...
  1. Sub 插入复制()
  2. Dim Ro As Long, n As Long
  3. Ro = 7
  4. ActiveSheet.Unprotect
  5. n = InputBox("输入行数", "请输入需要插入行的行数")
  6. Rows(Ro & ":" & Ro + n - 1).Insert
  7. Rows(Ro - 1).Copy Rows(Ro & ":" & Ro + n - 1)
  8. ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
  9. False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
  10. AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
  11. :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
  12. AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
  13. AllowUsingPivotTables:=True
  14. End Sub
复制代码

回复

使用道具 举报

 楼主| 发表于 2012-1-3 13:01 | 显示全部楼层
谢谢,已解决
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 07:36 , Processed in 0.270569 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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