Clear cell content with condition VBA
Mimi59
-
michel_m Posted messages 18903 Registration date Status Contributor Last intervention -
michel_m Posted messages 18903 Registration date Status Contributor Last intervention -
Hello,
I am a beginner in VBA and I would like to create a macro that clears the contents of a selection of cells if the value of a specified cell is equal to 0 (result of a formula);
However, even when it is non-zero, those cells are cleared, and I can't understand why;
Here is my macro:
Sub efface_var()
Dim MaValeur As Integer
MaValeur = Sheets("Date").Range("g10").Value
If MaValeur = 0 Then
Sheets("Synthèse").Select
Range("p32:p40,s32:s40,p90:p110,s90:s110,u90:u110,p152:p192,s152:s192,u152:u192").ClearContents
End If
End Sub
Thank you for your help, Read more
Configuration: Windows 7 / Internet Explorer 8.0
I am a beginner in VBA and I would like to create a macro that clears the contents of a selection of cells if the value of a specified cell is equal to 0 (result of a formula);
However, even when it is non-zero, those cells are cleared, and I can't understand why;
Here is my macro:
Sub efface_var()
Dim MaValeur As Integer
MaValeur = Sheets("Date").Range("g10").Value
If MaValeur = 0 Then
Sheets("Synthèse").Select
Range("p32:p40,s32:s40,p90:p110,s90:s110,u90:u110,p152:p192,s152:s192,u152:u192").ClearContents
End If
End Sub
Thank you for your help, Read more
Configuration: Windows 7 / Internet Explorer 8.0
3 answers
Hello,
Try this:
Good night
Try this:
Sub clear_var() Dim Range As Range Set Range = Sheets("Summary").Range("p32:p40,s32:s40,p90:p110,s90:s110,u90:u110,p152:p192,s152:s192,u152:u192") If Sheets("Date").Range("g10").Value = 0 Then Range.ClearContents End Sub Good night