简体   繁体   中英

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

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?

Thanks in advance!

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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