VBA : Macro qui tourne en boucle et bug

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 !

dionys0s

XLDnaute Impliqué
Bonjour le forum

voici mon soucis. J'ai un code dans ma feuil1, qui execute une macro dès qu'une cellule d'une plage est modifiée. Voici les codes :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub

If Target.Address = "$C$2" Then
Application.ScreenUpdating = False
    Call AdresseSociété

ElseIf Target.Address = "$C$4" Then
Application.ScreenUpdating = False
    Range("F8").ClearContents
    Call Liste_Valideurs
    Range("F6").Select

ElseIf Target.Address = "$F$6" Then
Application.ScreenUpdating = False
    Call TelFax

ElseIf Target.Address = "$F$8" Then
Application.ScreenUpdating = False
    Range("B23").Select

[B]ElseIf Not Intersect(Target, Range("$B$23:$B$46")) Is Nothing Then
Application.ScreenUpdating = False
Call Remplace
Target.Offset(0, 1).Select[/B]End If

End Sub

Il s'agit du dernier ElseIf

Voici le code de la macro :

Code:
Sub Remplace()

Application.ScreenUpdating = False

Fournisseur = ActiveCell.Value
Feuil6.Visible = True
Feuil6.Select
Columns("B:B").Select
Selection.Find(What:=Fournisseur, After:=ActiveCell, LookIn:=xlFormulas _
        , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Offset(0, -1).Activate
CodeFournisseur = ActiveCell.Value

Feuil6.Visible = False
Feuil1.Select

Cells.Replace What:=Fournisseur, Replacement:=CodeFournisseur, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

Application.ScreenUpdating = True

End Sub

En substance je comprends le probleme. Lors de la modification de la cellule, le remplacement se fait correctement, mais comme la cellule a été modifiée, il relance la macro, et comme il ne trouve aucune valeur définie par ma variable, il plante. Comment dois-je modifier le code pour faire comprendre à ma macro de ne rien faire si elle ne trouve pas les valeurs recherchées ?

Merci d'avance pour votre aide
 
Re : VBA : Macro qui tourne en boucle et bug

Bonjour dionys0s

A tester

VB:
.....
set c=Columns("B:B").Find(What:=Fournisseur, After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(0, -1).Activate
if not c is nothing then
CodeFournisseur =c.Value
 
Feuil6.Visible = False
Feuil1.Select
 
Cells.Replace What:=Fournisseur, Replacement:=CodeFournisseur, LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
 
End if
......
 
Re : VBA : Macro qui tourne en boucle et bug

Bonjour pierrejean

Ca ne marche pas. Ou alors ai-je fait une erreur.

mon code :
Code:
Sub Remplace()

Application.ScreenUpdating = False

Fournisseur = ActiveCell.Value
Feuil6.Visible = True
Feuil6.Select
Set c = Columns("B:B").Find(What:=Fournisseur, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, -1).Activate
If Not c Is Nothing Then
CodeFournisseur = c.Value
Feuil6.Visible = False
Feuil1.Select
Cells.Replace What:=Fournisseur, Replacement:=CodeFournisseur, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End If

Application.ScreenUpdating = True

End Sub

la macro bug sur cette ligne :

Code:
Set c = Columns("B:B").Find(What:=Fournisseur, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, -1).Activate
 
Re : VBA : Macro qui tourne en boucle et bug

Re

Ok

Modifie comme suit
Code:
[LEFT][FONT=monospace].....[/FONT]
[FONT=monospace][COLOR=#0000fc]set[/COLOR] c=Columns([COLOR=#800000]"B:B"[/COLOR]).Find(What:=Fournisseur, After:=ActiveCell, LookIn:=xlFormulas _[/FONT]
[FONT=monospace], LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _[/FONT]
[FONT=monospace]MatchCase:=[COLOR=#0000fc]False[/COLOR], SearchFormat:=[COLOR=#0000fc]False[/COLOR])[/FONT]
[FONT=monospace]i[/FONT][FONT=monospace][COLOR=#0000fc]f[/COLOR] [COLOR=#0000fc]not[/COLOR] c [COLOR=#0000fc]is[/COLOR] [COLOR=#0000fc]nothing[/COLOR] [COLOR=#0000fc]then[/COLOR][/FONT]
[FONT=monospace]CodeFournisseur =c.Offset(0, -1).Value[/FONT][/LEFT]
 
[LEFT][FONT=monospace]Feuil6.Visible = [COLOR=#0000fc]False[/COLOR][/FONT]
[FONT=monospace]Feuil1.[COLOR=#0000fc]Select[/COLOR][/FONT][/LEFT]
 
[LEFT][FONT=monospace]Cells.Replace What:=Fournisseur, Replacement:=CodeFournisseur, LookAt:= _[/FONT]
[FONT=monospace]xlPart, SearchOrder:=xlByRows, MatchCase:=[COLOR=#0000fc]False[/COLOR], SearchFormat:=[COLOR=#0000fc]False[/COLOR], _[/FONT]
[FONT=monospace]ReplaceFormat:=[COLOR=#0000fc]False[/COLOR][/FONT][/LEFT]
 
[LEFT][FONT=monospace][COLOR=#0000fc]End[/COLOR] [COLOR=#0000fc]if[/COLOR][/FONT]
[FONT=monospace]......[/FONT][/LEFT]
 
- 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

Réponses
7
Affichages
163
Réponses
2
Affichages
461
Réponses
1
Affichages
277
Retour