• Initiateur de la discussion Initiateur de la discussion Rick@
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

Rick@

XLDnaute Occasionnel
Re bonjour,

J'ai inserré une fonction dans mes macros mais voilà elle "bog"maintenant...

Il s'agit d'effacer le contenu d'une cellule quand le combobox1 est activé.

Comme le fichier est beaucoup trop volumineux voici la macro et en rouge l'ajout "fatale"... Merci de votre aide!

Sub Copie()
If Feuil3.ComboBox2 = "Commercial" Then
Rows("113:117").Copy Rows("94:94")
ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
Rows("119:123").Copy Rows("94:94")
ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
Rows("125:129").Copy Rows("94:94")
End If
If Feuil3.ComboBox2 = "Commercial" Then
Rows("148:151").Copy Rows("137:137")
ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
Rows("154:157").Copy Rows("137:137")
ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
Rows("160:163").Copy Rows("137:137")
End If
If Feuil3.ComboBox2 = "Commercial" Then
Rows("180:183").Copy Rows("169:169")
ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
Rows("186:189").Copy Rows("169:169")
ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
Rows("192:195").Copy Rows("169:169")
End If
If ComboBox3.ListIndex = -1 Then Exit Sub
On Error Resume Next
Application.Run ComboBox3
If Err.Number > 0 Then
MsgBox "Macro " & ComboBox3 & " inexistante"
End If
End Sub





Private Sub ComboBox1_Change()
Range("H12:I12").Select
Selection.ClearContents
End Sub

Private Sub ComboBox2_Change()
Call Copie
End Sub

Private Sub ComboBox3_Change()
Call Copie
End Sub
 
Re : Ajout à une macro.

Ta feuille est-elle protégé si oui est ce que les cellules que tu veux effacées sont -elles vérouillées?

Si ta feuille est protègé rajoute lors de l'ouverture un déprotection de ta feuille, puis justeaprès tu la reprotège en ajoutan le paramètre suivant :
UserInterfaceOnly=True.
 
Re : Ajout à une macro.

Bonsoir, Rick@

Y a-t-il une procédure événementielle dans le module de Feuil3 ?

PS : la liste des questions-réponses peut être long. Je gage pour ma part qu'un simple bout de fichier reproduisant l'erreur aurait déjà permis de te dépanner.
 
Re : Ajout à une macro.

Bonsoir,
tu as écris "Selection.ClearContentsEnd Sub" il manque le retour chariot avant "end sub" est-ce pareil dans ta macro ?
personnellement, je supprime sans selectionner, c'est plus simple:
Range("H12:I12").ClearContents
End Sub
cordialement
six'
 
Re : Ajout à une macro.

Bonsoir Rick@, salut nyko283,

Code:
Private Sub ComboBox1_Change()
Range("H12:I12").Select
Selection.ClearContents
End Sub
Cette macro nécessite que la feuille où se trouve ComboBox1 soit la feuille active.

Mais nul besoin de l'activer.

Si le nom de cette feuille est "toto", écrivez donc plus simplement :

Code:
Private Sub ComboBox1_Change()
Sheets("toto").Range("H12:I12").ClearContents
End Sub
ou encore plus simplement :

Code:
Private Sub ComboBox1_Change()
Me.Range("H12:I12").ClearContents
End Sub
Par ailleurs, je suis étonné qu'il ne vous saute pas aux yeux ceci : les mêmes tests se répètent 3 fois (!!) dans votre macro Copie.

Ceci sera tout de même mieux :

Code:
Sub Copie()
If Feuil3.ComboBox2 = "Commercial" Then
  Rows("113:117").Copy Rows(94)
  Rows("148:151").Copy Rows(137)
  Rows("180:183").Copy Rows(169)
ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
  Rows("119:123").Copy Rows(94)
  Rows("154:157").Copy Rows(137)
  Rows("186:189").Copy Rows(169)
ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
  Rows("125:129").Copy Rows(94)
  Rows("160:163").Copy Rows(137)
  Rows("192:195").Copy Rows(169)
End If
If ComboBox3.ListIndex = -1 Then Exit Sub
On Error Resume Next
Application.Run ComboBox3
If Err Then MsgBox "Macro " & ComboBox3 & " inexistante"
End Sub

Edit : bonsoir Victor21, sixair

Bonne fin de soirée.
 
Dernière édition:
Re : Ajout à une macro.

Bonjour Barbatruc,

Gros merci pour l'ordre que tu as mis dans mon "fouilli" cela fonctionne très bien quand j'actionne le combo1 mais quand j'actionne les deux autres combo2 et 3 le même message d'erreur apparait...Soit: Erreur d'exécution '1004'
La méthode Select de la classe Range a échoué.
Que faire???
 
Re : Ajout à une macro.

Bonjour Rick@, le fil, le forum,

Il me semble que mon post #9 est clair :

- ne pas utiliser Select

- préciser la feuille devant chaque Range.

Et préciser aussi la feuille devant chaque ComboBox si elle est bien dans une feuille.

A+
 
Re : Ajout à une macro.

Re,

Et les Rows étant des Range, il faut aussi préciser la feuille ("toto") :

Code:
Sub Copie()
With Sheets("toto") 'à adapter
  If Feuil3.ComboBox2 = "Commercial" Then
    .Rows("113:117").Copy .Rows(94)
    .Rows("148:151").Copy .Rows(137)
    .Rows("180:183").Copy .Rows(169)
  ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
    .Rows("119:123").Copy .Rows(94)
    .Rows("154:157").Copy .Rows(137)
    .Rows("186:189").Copy .Rows(169)
  ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
    .Rows("125:129").Copy .Rows(94)
    .Rows("160:163").Copy .Rows(137)
    .Rows("192:195").Copy .Rows(169)
  End If
  If .ComboBox3.ListIndex = -1 Then Exit Sub
  On Error Resume Next
  Application.Run .ComboBox3
  If Err Then MsgBox "Macro " & .ComboBox3 & " inexistante"
End With
End Sub
Edit : précision : on peut remplacer Sheets("toto") par le CodeName (Feuil1, Feuil2 etc), c'est souvent préférable en effet.

A+
 
Dernière édition:
Re : Ajout à une macro.

Re,

Mais peut-être que les copies des Rows doivent se faire dans la feuille de chaque ComboBox ( 2 feuilles différentes) ??

Dans ce cas, il faut paramétrer la macro :

Code:
Sub Copie(W As Worksheet)
With W
  If Feuil3.ComboBox2 = "Commercial" Then
    .Rows("113:117").Copy .Rows(94)
    .Rows("148:151").Copy .Rows(137)
    .Rows("180:183").Copy .Rows(169)
  ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
    .Rows("119:123").Copy .Rows(94)
    .Rows("154:157").Copy .Rows(137)
    .Rows("186:189").Copy .Rows(169)
  ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
    .Rows("125:129").Copy .Rows(94)
    .Rows("160:163").Copy .Rows(137)
    .Rows("192:195").Copy .Rows(169)
  End If
  If xxxxx.ComboBox3.ListIndex = -1 Then Exit Sub 'xxxxx => CodeName de la feuille contenant ComboBox3
  On Error Resume Next
  Application.Run xxxxx.ComboBox3
  If Err Then MsgBox "Macro " & xxxxx.ComboBox3 & " inexistante"
End With
End Sub
qui doit être appelée ainsi :

Code:
Private Sub ComboBox2_Change()
Copie Me
End Sub

Private Sub ComboBox3_Change()
Copie Me
End Sub
Me est envoyé à la macro comme argument.

A+
 
Dernière édition:
Re : Ajout à une macro.

Re,

Et maintenant... ne faut-il pas paramétrer aussi avec chaque ComboBox ??

Alors :

Code:
Sub Copie(W As Worksheet, combo As Object)
With W
  If Feuil3.ComboBox2 = "Commercial" Then
    .Rows("113:117").Copy .Rows(94)
    .Rows("148:151").Copy .Rows(137)
    .Rows("180:183").Copy .Rows(169)
  ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
    .Rows("119:123").Copy .Rows(94)
    .Rows("154:157").Copy .Rows(137)
    .Rows("186:189").Copy .Rows(169)
  ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
    .Rows("125:129").Copy .Rows(94)
    .Rows("160:163").Copy .Rows(137)
    .Rows("192:195").Copy .Rows(169)
  End If
  If combo.ListIndex = -1 Then Exit Sub
  On Error Resume Next
  Application.Run combo
  If Err Then MsgBox "Macro " & combo & " inexistante"
End With
End Sub
Code:
Private Sub ComboBox2_Change()
Copie Me, ComboBox2
End Sub

Private Sub ComboBox3_Change()
Copie Me, ComboBox3
End Sub
A+
 
Re : Ajout à une macro.

Bonjour Victor21,

Merci de ta patience (model).

Bon j'ai bien essayé mais comme tu as surement vu, je suis un peu limité dans VBA. Donc, voici maintenant le message (GIF) que j'obtiens en utilisant les combo 2 et 3. La 1 fonctionne bien.

Sub Copie(W As Worksheet, combo As Object)
With W
If Feuil3.ComboBox2 = "Commercial" Then
.Rows("113:117").copy .Rows(94)
.Rows("148:151").copy .Rows(137)
.Rows("180:183").copy .Rows(169)
ElseIf Feuil3.ComboBox2 = "Résidentiel" Then
.Rows("119:123").copy .Rows(94)
.Rows("154:157").copy .Rows(137)
.Rows("186:189").copy .Rows(169)
ElseIf Feuil3.ComboBox2 = "Recouvrement" Then
.Rows("125:129").copy .Rows(94)
.Rows("160:163").copy .Rows(137)
.Rows("192:195").copy .Rows(169)
End If
If combo.ListIndex = -1 Then Exit Sub
On Error Resume Next
Application.Run combo
If Err Then MsgBox "Macro " & combo & " inexistante"
End With
End Sub

Private Sub ComboBox1_Change()
Me.Range("H12:I12").ClearContents
End Sub



Private Sub ComboBox2_Change()
Call Copie
End Sub

Private Sub ComboBox3_Change()
Call Copie
End Sub
 

Pièces jointes

  • Erreur.gif
    Erreur.gif
    10.4 KB · Affichages: 70
  • Erreur.gif
    Erreur.gif
    10.4 KB · Affichages: 76
  • Erreur.gif
    Erreur.gif
    10.4 KB · Affichages: 75
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Retour