Clear cell content with condition VBA

Mimi59 -  
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

3 answers

cousinhub29 Posted messages 1112 Registration date   Status Member Last intervention   383
 
Hello,

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
1
mimi59
 
Thank you, but even like this when the value of cell G10 is >0, the range of values is erased, I don't understand what's wrong. It should work!!! I'm losing my mind, I need to submit my file but the macro is acting up!!!
0
michel_m Posted messages 18903 Registration date   Status Contributor Last intervention   3 320
 
Hello

I just tried your macro: no problems

maybe check if G10 is numeric and not a date...

otherwise, I don't see
--
Michel
0