VBA : Macro qui tourne en boucle et bug

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
 

pierrejean

XLDnaute Barbatruc
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
......
 

dionys0s

XLDnaute Impliqué
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
 

pierrejean

XLDnaute Barbatruc
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]
 

dionys0s

XLDnaute Impliqué
Re : VBA : Macro qui tourne en boucle et bug

Je dois me tromper quelquepart mais ça ne marche pas...

Je reviendrai dessus quand j'aurai du temps. En attendant je ferai avec la solution de coco lapin.

Merci beaucoup à vous deux en tout cas !
 

Discussions similaires

Réponses
1
Affichages
144

Statistiques des forums

Discussions
312 757
Messages
2 091 774
Membres
105 070
dernier inscrit
metalfer