vba 使用宏清除工作表的内容,而不清除剪贴板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11727620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear contents of a WorkSheet using macro, without clearing the clipboard
提问by Kiranshell
How do I clear the contents of a excel sheet using macro, without clearing the contents of the clipboard?
如何使用宏清除 Excel 工作表的内容,而不清除剪贴板的内容?
I am currently using the below code(which is called by clicking a button on the sheet), but this clears the data in the clipboard, which I have copied from other source, and want to paste in the cleared sheet
我目前正在使用以下代码(通过单击工作表上的按钮调用),但这会清除剪贴板中的数据,我从其他来源复制了该数据,并希望粘贴到已清除的工作表中
Sub clearly()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.UsedRange.ClearContents
Next ws
End Sub
回答by Siddharth Rout
which I have copied from other source, and want to paste in the cleared sheet
我从其他来源复制的,并想粘贴到已清除的工作表中
Why not create a temp sheet and paste the data there and then clear all the sheets. Once done copy the data from the temp sheet to the relevant sheet and delete the temp sheet?
为什么不创建一个临时表并将数据粘贴到那里,然后清除所有的表。完成后将数据从临时表复制到相关表并删除临时表?
See this (TRIED AND TESTED)
看到这个(尝试和测试)
Sub clearly()
Dim ws As Worksheet, wsTemp As Worksheet
'~~> Create a Temp Sheet
Set wsTemp = Sheets.Add
'~~> Copy clipboad data to temp sheet
wsTemp.Range("A1").PasteSpecial xlPasteAll
'~~> Clear contents of all sheets except temp sheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> wsTemp.Name Then ws.Cells.ClearContents
Next ws
'~~> Copy data from temp sheet to relevant sheet
wsTemp.Cells.Copy Sheets("Sheet1").Cells
'~~> Delete temp sheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub