简体   繁体   English

宏以复制一行,将其插入下一个空白行并清除内容

[英]Macro to copy a row, insert it into next blank row and clear contents

I need the VBA code to do this: 我需要VBA代码来执行此操作:

Copy a row 复制一行

insert the copied row into the next blank row 将复制的行插入下一个空白行

clear the contents (without deleting the formula or data validation that's in the cells). 清除内容(不删除单元格中的公式或数据验证)。

I'm currently using this, but it only inserts the row directly underneath the active cell and doesn't clear the contents. 我当前正在使用它,但是它仅将行直接插入活动单元格的下方,并且不会清除内容。

Sub InsertCopyRow()

   ActiveCell.Offset(1,0).EntireRow.Insert
   ActiveCell.EntireRow.Copy ActiveCell.Offset(1,0).EntireRow

End Sub

Thanks! 谢谢!

Two solutions, pick the one you like: 两种解决方案,选择您喜欢的一种:

Use Cut instead of Copy: 使用剪切而不是复制:

Sub InsertCopyRow()

 ActiveCell.Offset(1, 0).EntireRow.Insert
 ActiveCell.EntireRow.Cut ActiveCell.Offset(1, 0).EntireRow

End Sub

Or clear the contents of the original row after copying: 或在复制后清除原始行的内容:

Sub InsertCopyRow()

 ActiveCell.Offset(1, 0).EntireRow.Insert
 ActiveCell.EntireRow.Copy ActiveCell.Offset(1, 0).EntireRow
 ActiveCell.EntireRow.ClearContents

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM