XL 2016 Bouton effacer des cellules en particuliers

Duam

XLDnaute Nouveau
Bonjour,

J'ai un fichier avec une feuille de saisie avec un premier bouton qui copie colle dans une autre feuille.
Je dois maintenant créer un bouton pour effacer le contenu des cellules. Sachant que certaines cellules ont des calculs ou listes déroulantes.
J'ai essayé un premier code mais il ne fonctionne pas, pouvez-vous m'aider svp?
VB:
Sub Effacer_feuille_saisie()

Cells("M4").ClearContents
Cells("C3").ClearContents
Cells("C5").ClearContents
Cells("I7").ClearContents
Cells("C11").ClearContents
Cells("I5").ClearContents
Cells("I3").ClearContents
Cells("C13").ClearContents
Cells("I9").ClearContents
Cells("I11").ClearContents
Cells("P9").ClearContents
Cells("P13").ClearContents
Cells("M9").ClearContents
Cells("I13").ClearContents
Cells("C20").ClearContents
Cells("B20").ClearContents
Cells("D20").ClearContents
Cells("E20").ClearContents
Cells("F20").ClearContents
Cells("G20").ClearContents
Cells("H20").ClearContents
Cells("I20").ClearContents
Cells("J20").ClearContents
Cells("K20").ClearContents
Cells("L20").ClearContents
Cells("M20").ClearContents
Cells("B27").ClearContents
Cells("E20").ClearContents
Cells("C27").ClearContents
Cells("H27").ClearContents
Cells("L27").ClearContents
Cells("K27").ClearContents
Cells("I27").ClearContents
Cells("M27").ClearContents
Cells("N27").ClearContents
Cells("P27").ClearContents

End Sub
 

Pièces jointes

  • OBSERVATOIRE DE LA QUALITE_SRP_MODELE SAISIE DONNEES.xlsm
    390.5 KB · Affichages: 7

vgendron

XLDnaute Barbatruc
Hello

il me semble te l'avoir déjà dit..? (excuse si ce n'est pas à toi)
mais une cellule excel, c'est soit par Cells, soit par range
pour Cells, on spécifie ligne et colonne==> Cells(ligne,colonne)
pour Range: on donne le nom de la cellule "A1"
il peut y avoir des cas particuliers de mix, mais globalement, ce sont ces deux syntaxes

==> la cellule A1 c'est soit Range("A1") soit cells(1,1), mais pas Cells("A1")
 

Duam

XLDnaute Nouveau
Hello

il me semble te l'avoir déjà dit..? (excuse si ce n'est pas à toi)
mais une cellule excel, c'est soit par Cells, soit par range
pour Cells, on spécifie ligne et colonne==> Cells(ligne,colonne)
pour Range: on donne le nom de la cellule "A1"
il peut y avoir des cas particuliers de mix, mais globalement, ce sont ces deux syntaxes

==> la cellule A1 c'est soit Range("A1") soit cells(1,1), mais pas Cells("A1")
J'ai essayé avec Range et cela ne fonctionne pas non plus, dès la première ligne j'ai une erreur 1004
 

Pièces jointes

  • OBSERVATOIRE DE LA QUALITE_SRP_MODELE SAISIE DONNEES.xlsm
    390.5 KB · Affichages: 1

vgendron

XLDnaute Barbatruc
il faudrait aussi regrouper les zones à effacer et les mettre dans l'ordre.. à mon avis.. tu en as oublié
VB:
Sub Effacer_feuille_saisie()

Range("C3").ClearContents
Range("C5").ClearContents
Range("C11").ClearContents
Range("C13").ClearContents

Range("I3").ClearContents
Range("I5").ClearContents
Range("I7").ClearContents
Range("I9").ClearContents
Range("I11").ClearContents
Range("I13").ClearContents

Range("P9").ClearContents
Range("P13").ClearContents


Range("B20:M20").ClearContents

Range("B27:C27").ClearContents
Range("H27:I27").ClearContents
Range("K27:P27").ClearContents

End Sub
 

Duam

XLDnaute Nouveau
Je veux juste effacer le chiffre et pas la formule pour que la feuille soit vierge.
Je dois défusionner- effacer- refusionner pour ce genre de cellule?

VB:
Sub Effacer_feuille_saisie()

Range("C3").ClearContents
Range("C5").ClearContents
Range("C7").ClearContents
Range("C9").ClearContents
Range("C11").ClearContents
Range("C13").ClearContents

Range("I3").ClearContents
Range("I5").ClearContents
Range("I7").ClearContents
Range("I9").ClearContents
Range("I11").ClearContents
Range("I13").ClearContents

'Effacer chiffre pas formule
Range("M4").ClearContents
Range("M9").ClearContents

Range("M13").ClearContents

Range("P5").ClearContents
Range("P9").ClearContents
Range("P13").ClearContents

'Effacer chiffre pas formule
Range("B20:N20").ClearContents
Range("H20").ClearContents

'Effacer chiffre pas formule
Range("B27:C27").ClearContents
Range("H27").ClearContents
Range("H27:I27").ClearContents
Range("K27:P27").ClearContents
Range("P27").ClearContents

End Sub
 

vgendron

XLDnaute Barbatruc
non ca ne va pas... tu as un souci de compréhension..
le chiffre que tu souhaites effacer.. il vient d'ou..? de la formule
la cellule.. elle contient une formule==> si tu veux "effacer" le chiffre: c'est à dire ne RIEN voir, il faut alors modifier la formule pour qu'elle ne renvoie rien
exemple
en A1 : 10 la.. il s'agit d'une valeur
en B1: =si(A1<>"";A1;"") ici, il s'agit d'une formule qui renvoie une valeur SI il y a une valeur en A1, rien sinon
 

Duam

XLDnaute Nouveau
Effectivement j'avais mal compris, excusez moi!
Du coup j'ai enlever les cellules contenant les formules du VBA;
Il me reste juste une cellule fusionnée que je ne peux effacer
VB:
Sub Effacer_feuille_saisie()

Range("C3").ClearContents
Range("C5").ClearContents
Range("C7").ClearContents
Range("C9").ClearContents
Range("C11").ClearContents
Range("C13").ClearContents

Range("I3").ClearContents
Range("I5").ClearContents
Range("I7").ClearContents
Range("I9").ClearContents
Range("I11").ClearContents
Range("I13").ClearContents

'Cellule fusionnée erreur 1004
Range("M13").ClearContents

Range("P5").ClearContents
Range("P9").ClearContents
Range("P13").ClearContents

Range("B20:G20").ClearContents
Range("I20:N20").ClearContents

Range("B27:G27").ClearContents
Range("I27:O27").ClearContents

End Sub
 

vgendron

XLDnaute Barbatruc
VB:
Sub Effacer_feuille_saisie()

Range("C3").ClearContents
Range("C5").ClearContents
Range("C7").ClearContents
Range("C9").ClearContents
Range("C11").ClearContents
Range("C13").ClearContents

Range("I3").ClearContents
Range("I5").ClearContents
Range("I7").ClearContents
Range("I9").ClearContents
Range("I11").ClearContents
Range("I13").ClearContents

Range("M13:N13").ClearContents

Range("P5").ClearContents
Range("P9").ClearContents
Range("P13").ClearContents

Range("B20:G20").ClearContents
Range("I20:P20").ClearContents

Range("B27:G27").ClearContents
Range("I27:O27").ClearContents

End Sub
 

Eric C

XLDnaute Barbatruc
Bonjour le forum
Bonjour Duam, Vincent, TooFatBoy

Notre ami vgendron ayant fait tout le travail de tri, je me propose de simplifier le code de ton "CommandButton2" (Effacement) avec ce code :
VB:
Private Sub CommandButton2_Click()
 Application.ScreenUpdating = False

For i = 3 To 13 Step 2
Range("C" & i).ClearContents
 Next i

For j = 3 To 13 Step 2
Range("I" & j).ClearContents
Next j

For k = 5 To 13 Step 4
Range("P" & k).ClearContents
Next k

For l = 5 To 13 Step 4
Range("P" & l).ClearContents
Next l

Union([M13:N13], [B20:G20], [I20:P20], [B27:G27], [I27:O27]).ClearContents
 
 Application.ScreenUpdating = True
 
End Sub
Bonne après-midi
@+ Eric c
 

vgendron

XLDnaute Barbatruc
salut @Eric C
tu aurais quand meme pu cumuler les boucles :):)
et pourquoi deux fois la boucle sur k et sur l?
VB:
Private Sub CommandButton2_Click()
 Application.ScreenUpdating = False

For i = 3 To 13 Step 2
Range("C" & i).ClearContents
Range("I" & i).ClearContents
 Next i

For k = 5 To 13 Step 4
Range("P" & k).ClearContents
Next k

Union([M13:N13], [B20:G20], [I20:P20], [B27:G27], [I27:O27]).ClearContents
 
 Application.ScreenUpdating = True
 
End Sub
 

Statistiques des forums

Discussions
312 104
Messages
2 085 346
Membres
102 868
dernier inscrit
JJV