vba Excel:ClearContent 保留公式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4430755/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:59:43  来源:igfitidea点击:

Excel: ClearContent preserving formulas

excelexcel-vbavba

提问by Lorenzo

In Excel VBA is there a way to safely use Range("A1:B2").ClearContentwithout deleting the existent formulas in the cells?

在 Excel VBA 中,有没有一种方法可以Range("A1:B2").ClearContent在不删除单元格中现有公式的情况下安全使用?

Just to make my scenario clearer: I'm pulling fresh data from the database and just want to erase everything on the sheet but not the formulas because those will resolve some data in other fields with vlookups.

只是为了让我的场景更清晰:我从数据库中提取新数据,只想擦除工作表上的所有内容而不是公式,因为这些将使用 vlookups 解析其他字段中的一些数据。

回答by Dick Kusleika

Use the SpecialCells property to get only the constant values.

使用 SpecialCells 属性仅获取常量值。

Sub RemoveConstants()

    Dim rConstants As Range

    Set rConstants = Sheet1.Range("A1:B2").SpecialCells(xlCellTypeConstants)
    rConstants.ClearContents

End Sub