VBA / Suppression d'une colonne si sa somme est nulle

Résolu
Oldevi Messages postés 10 Date d'inscription mardi 2 décembre 2008 Statut Membre Dernière intervention 28 janvier 2009 - 4 déc. 2008 à 16:05
Oldevi Messages postés 10 Date d'inscription mardi 2 décembre 2008 Statut Membre Dernière intervention 28 janvier 2009 - 5 déc. 2008 à 23:48
Bonjour,

Je voudrais supprimer des colonnes sur une plage donnees si la somme des cellules qui la compose est nulle.

J'ai tente ca

For Each Column In Range("A:K")
    If Application.Sum(EntireColumn) = 0 Then EntireColumn.Delete Shift:=xlToLeft
Next Column

Mais ca ne fonctionne pas, pas de bug, mais ca ne fait rien.

Merci d'avance !!

4 réponses

vinou19 Messages postés 10 Date d'inscription lundi 1 décembre 2008 Statut Membre Dernière intervention 15 juin 2010
5 déc. 2008 à 13:24
salut oldevi,


<meta http-equiv= "Content-Type" content="text/html; charset=utf-8" />
<meta name="ProgId" content="Word.Document" />
<meta name="Generator" content="Microsoft Word 11" />
<meta name="Originator" content="Microsoft Word 11" />
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CSylvie%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:536871559 0 0 0 415 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:49.6pt;
margin-bottom:.0001pt;
text-align:justify;
line-height:150%;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:Verdana;
mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Times New Roman";}
@page Section1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Tableau Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="ProgId" content="Word.Document" />
<meta name="Generator" content="Microsoft Word 11" />
<meta name="Originator" content="Microsoft Word 11" />
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CSylvie%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount= "156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:536871559 0 0 0 415 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:49.6pt;
margin-bottom:.0001pt;
text-align:justify;
line-height:150%;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:Verdana;
mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Times New Roman";}
@page Section1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Tableau Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->

Voici ce que je peux te répondre sur ta question au sujet de
la suppression des colonnes nulles.




 






Tout d'abord, je déconseille absolument de déterminer une zone d'application sous la
forme Range ("A:K"), c'est trop statique et surtout trop aléatoire si
on a besoin d'insérer ou de supprimer une colonne dans cette plage par exemple,
ça fausse tout.



Dans la plupart des cas de figure, il vaut mieux travailler
sur une ou sur la zone en cours




 






Ensuite, il ne faut pas confondre ce qui suit :
"entirecolumn" est une propriété
qui s'applique sur un objet "range" et non un objet sur lequel on
peut travailler.




 







De plus, il ne faut jamais oublier qu'avant
d'entreprendre une action, il faut SELECTIONNER
l'élément sur lequel s'applique cette action.




 







Voici 2 exemples qui fonctionnent :




 







'cas1 : zone nommée ("zone_dapplication")




Sub
SUPRESSION_COLONNE()







For Each Column In
Range("zone_dapplication")








Column.EntireColumn.Select








If Application.Sum(Selection) = 0
Then Selection.Delete shift:= xlToLeft







Next Column







End Sub








 








'cas2 : zone en cours



Sub SUPRESSION_COLONNE()




Range("A1").Select







For Each Column in Selection.CurrentRegion








Column.EntireColumn.Select








If Application.Sum(Selection) = 0
Then Selection.Delete shift:=xlToLeft







Next Column







End Sub








 







Voilà, j'espère avoir à peu près répondu à ta question,






 








Bon courage, vinou19
3
vinou19 Messages postés 10 Date d'inscription lundi 1 décembre 2008 Statut Membre Dernière intervention 15 juin 2010
4 déc. 2008 à 17:00
bonsoir oldevi,

Je m'occupe de vous répondre dès demain.

Mais à mon avis, la réponse est dans : "faire systématiquement une sélection de quelque chose avant de tenter une action".
Et il n'y a pas de sélection dans votre essai

Cordialement, vinou19
0
Oldevi Messages postés 10 Date d'inscription mardi 2 décembre 2008 Statut Membre Dernière intervention 28 janvier 2009
4 déc. 2008 à 17:08
Merci Vinoul pour votre reponse,

Je rectifie, ca ne fait pas rien, en fait ca supprime les colonnes concernees sans tenir compte de la somme.

Cdt,

oldevi
0
Oldevi Messages postés 10 Date d'inscription mardi 2 décembre 2008 Statut Membre Dernière intervention 28 janvier 2009
5 déc. 2008 à 23:48
Merci beaucoup Vinou,

J'avais tente ca qui marchait a peu pres (bizarrement il oubliait des colonnes, peut etre pour la raison que tu cites plus haut)
   
   Dim col as range

    For Each Col In Range("G1:AP1")
    If Application.Sum(Col.EntireColumn) < 1 Then Col.EntireColumn.Delete shift:=xlToLeft
    Next Col

Bon WE !
0
Rejoignez-nous