vba 如何删除excel vba中具有特定值的每个单元格?

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

How can I delete every cell with a specific value in excel vba?

excelvbaexcel-vba

提问by kanbhold

I would like to delete every zeros in a worksheet.

我想删除工作表中的每个零。

The zeros are spread out random in my worksheet.

零在我的工作表中随机分布。

How can I easily delete the zero cells with VBA?

如何使用 VBA 轻松删除零单元格?

Thanks in advance!

提前致谢!

回答by Dan Donoghue

This will replace all zeros with nothing:

这将用空替换所有零:

Sub ZeroToBlank()
ActiveSheet.UsedRange.Replace 0, "", lookat:=xlWhole
End Sub

Updated as per comments from Jeeped. Nice catch Jeeped.

根据 Jeeped 的评论更新。不错的抓获吉普车。

回答by Dan Donoghue

You might try,

你可以试试,

sub hide_Zeroes
    ActiveWindow.DisplayZeros = False
end sub

While this does not actually clear the zero values, it removes them from view. It also hides zeroes that are the result of formulas while not removing the formulas.

虽然这实际上并没有清除零值,但它会将它们从视图中删除。它还隐藏作为公式结果的零而不删除公式。

回答by BrakNicku

Code to remove zeros in active sheet:

在活动工作表中删除零的代码:

Sub RemoveZeros()

Dim Rng As Range

For Each Rng In ActiveSheet.UsedRange
  If Rng.Value2 = 0 Then Rng.ClearContents
Next Rng

End Sub