Microsoft 365 Find : si texte recherché n'existe pas alors .....

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour à toutes et à tous,
Je vous souhaite un beau dimanche :)

Encore un code que je n'arrive pas à faire Grrrrrr !!!!! 'nul que je suis)

En recherche find, je voudrais gérer si l'objet de ma recherche n'existe pas.
Par exemple, je cherche "le forum"
Il n'existe pas ... mon code,
else
Il existe................ mon code

J'ai fait des tests et des recherches sans y parvenir.
Voici le code où j'en suis (code qui beug :mad:) :
VB:
Sheets("RdV_transfert").Select
'Columns("L:L").Select
    Dim x As Range
    Set x = Range("l:l").Find("à confirmer", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
 
'    Columns("L:L").Select
'    Selection.Find(What:="à confirmer", After:=ActiveCell, LookIn:=xlValues, _
'        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
'        MatchCase:=False, SearchFormat:=False).Activate
'
' SI TROUVE PAS
[a1].Select
    Sheets("SMS RdV").Select
    MsgBox ("Y'en n'a pas ou plus")
    Application.EnableEvents = True
    Application.ScreenUpdating = True

' SI TROUVE
            ActiveCell = "SMS envoyé"
            Sheets("SMS RdV").Range("c7") = ActiveCell.Offset(0, -4) 'Client
            Sheets("SMS RdV").Range("b6") = ActiveCell.Offset(0, -3) 'Client téléphone
            Sheets("SMS RdV").Range("g4") = ActiveCell.Offset(0, -2) 'Commerciale
            Sheets("SMS RdV").Range("d4") = ActiveCell.Offset(0, -1) 'Commerciale téléphone
            Sheets("SMS RdV").Range("c10") = ActiveCell.Offset(0, -10) 'date RdV
            Sheets("SMS RdV").Range("c11") = ActiveCell.Offset(0, -9) 'date appel
            Sheets("SMS RdV").Range("c27") = ActiveCell.Offset(0, -4) 'Réseau
            Sheets("SMS RdV").Range("c28") = ActiveCell.Offset(0, -6) 'Prospect
            Sheets("SMS RdV").Range("c29") = ActiveCell.Offset(0, -5) 'Téléphone

            Sheets("SMS RdV").Select
            ActiveSheet.Shapes("bouton 1").Visible = True
            Application.EnableEvents = True
            Application.ScreenUpdating = True

End Sub

Auriez-vous le bon code ?
Si besoin, je ferai un p'tit fichier test.

Je vous remercie comme d'habitude et je continue mes recherches,
Amicalement,
lionel :)
 
Dernière édition:

fanch55

XLDnaute Barbatruc
Salut Lionel,
Set x = Range("l:l").Find("à confirmer", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Quand tu fais un Find, il ne faut pas faire un activate car tu ne sais pas encore si la valeur va être trouvée --> erreur d'exécution .
VB:
    Set x = Range("l:l").Find("à confirmer", After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
    If X is nothing then
        ...
    Else
        ...
    End if
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour fanch55, Bonjour laurent950,

Merci à vous deux pour m'avoir donné la solution :)
VB:
Sub RdV_jour()
Sheets("RdV_transfert").Select
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim x As Range

With Sheets("RdV_transfert").Range("l:l")
    Set x = .Find(What:="à confirmer", After:=Range("L1"), LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
End With
On Error Resume Next
x.Activate
    'Test pour générer une erreur si la variable est Nothing
    'x.Worksheet.Select ' Selection de la feuille "RdV_transfert"
If Err <> 0 Then
' SI TROUVE PAS
    [a1].Select
    Sheets("SMS RdV").Select
    MsgBox ("Y'en n'a pas ou plus")
    ActiveSheet.Shapes("bouton 1").Visible = False
    Application.EnableEvents = True
    Application.ScreenUpdating = True
        Else
        ' SI TROUVE
        ActiveCell.Value = "SMS envoyé"
        Sheets("SMS RdV").Range("c7") = ActiveCell.Offset(0, -4) 'Client
        Sheets("SMS RdV").Range("b6") = x.ActiveCell(0, -3) 'Client téléphone
        Sheets("SMS RdV").Range("g4") = ActiveCell.Offset(0, -2) 'Commerciale
        Sheets("SMS RdV").Range("d4") = ActiveCell.Offset(0, -1) 'Commerciale téléphone
        Sheets("SMS RdV").Range("c10") = ActiveCell.Offset(0, -10) 'date RdV
        Sheets("SMS RdV").Range("c11") = ActiveCell.Offset(0, -9) 'date appel
        Sheets("SMS RdV").Range("c27") = ActiveCell.Offset(0, -4) 'Réseau
        Sheets("SMS RdV").Range("c28") = ActiveCell.Offset(0, -6) 'Prospect
        Sheets("SMS RdV").Range("c29") = ActiveCell.Offset(0, -5) 'Téléphone
            
        Sheets("SMS RdV").Select
        ActiveSheet.Shapes("bouton 1").Visible = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
End If
On Error GoTo 0  
End Sub
lionel :)
 
Dernière édition:

laurent950

XLDnaute Accro
Bonjour @Usine à gaz , @fanch55

j'avais écrit à @Usineàgaz directement en message car je n'ai pas su résoudre son poste j'ai détaillé le code que je suis arrivé a corrigé jusqu'à une certaine limite :

je le poste ici, j'ai modifié la gestion de l'Erreur @fanch55 si tu as une solution autre je suis preneur pour apprendre aussi. je te remercie @fanch55

After:=ActiveCell ne fonctionne pas remplacer par After:=Range("L1")

VB:
Sub test()
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim x As Range
With Sheets("RdV_transfert").Range("l:l")
'Columns("L:L").Select
    Set x = .Find(What:="à confirme", After:=Range("L1"), LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
End With

On Error GoTo RechercheDateSurTouteColonneL
' Test pour générer une erreur si la variable est Nothing
    x.Worksheet.Select ' Selection de la feuille "RdV_transfert"
' SI TROUVE
           ' Pour moi ActiveCell = x
            'ActiveCell.Value = "SMS envoyé"
            x.Value = "SMS envoyé"
            Sheets("SMS RdV").Range("c7") = x.Offset(0, -4) 'Client
            Sheets("SMS RdV").Range("b6") = x.Offset(0, -3) 'Client téléphone
            Sheets("SMS RdV").Range("g4") = x.Offset(0, -2) 'Commerciale
            Sheets("SMS RdV").Range("d4") = x.Offset(0, -1) 'Commerciale téléphone
            Sheets("SMS RdV").Range("c10") = x.Offset(0, -10) 'date RdV
            Sheets("SMS RdV").Range("c11") = x.Offset(0, -9) 'date appel
            Sheets("SMS RdV").Range("c27") = x.Offset(0, -4) 'Réseau
            Sheets("SMS RdV").Range("c28") = x.Offset(0, -6) 'Prospect
            Sheets("SMS RdV").Range("c29") = x.Offset(0, -5) 'Téléphone

            Sheets("SMS RdV").Select
            ActiveSheet.Shapes("bouton 1").Visible = True

Application.EnableEvents = True
Application.ScreenUpdating = True

RechercheDateSurTouteColonneL:
' SI TROUVE PAS
    Sheets("SMS RdV").Select
    MsgBox ("Y'en n'a pas ou plus")
End Sub
 
Dernière édition:

Discussions similaires

Réponses
2
Affichages
129
Réponses
2
Affichages
727

Statistiques des forums

Discussions
311 721
Messages
2 081 929
Membres
101 843
dernier inscrit
Thaly