Erreur 91 : variable objet ou de bloc with non définie

Bonjour au forum,

Je rencontre une erreur Eerreur 91 : variable objet ou de bloc with non définie" sur la ligne

RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(what:=UtilisateurActuel, LookIn:=xlValues, lookat:=xlWhole)

de ce code, qui me permet de tester à l'ouverture du fichier si l'utilisateur a déjà pris connaissance du texte de l'userform USF_Avertissement.

Je n'arrive pas à comprendre pourquoi...

Sub ControlVisa()

Dim dl As Long
Dim RechercheVisa As Range
Dim StatutVisa As String
Dim UtilisateurActuel As String

dl = Sheets("BD").Range("M" & Rows.Count).End(xlUp).Row

UtilisateurActuel = UCase(Environ("username"))
RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(what:=UtilisateurActuel, LookIn:=xlValues, lookat:=xlWhole)
StatutVisa = RechercheVisa.Offset(0, 1).Value

If Not RechercheVisa Is Nothing Then 'condition : s'il existe au moins une occurrence trouvée
    If StatutVisa = "Oui" Then Exit Sub
    If StatutVisa = "Non" Or StatutVisa = "" Then USF_Avertissement.Show
Else:
    With Sheets("BD")
        .Unprotect "mdp"
        .Range("M:" & dl + 1).Value = UCase(Environ("username"))
        .Protect "mdp"
    End With
End If

End Sub

Des idées ?

Merci d'avance pour votre aide

Bonjour,

Tu définis :

Dim RechercheVisa As Range

Et tu recherches un "String"...

RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(what:=UtilisateurActuel, LookIn:=xlValues, lookat:=xlWhole)

Essaie en rajoutant "Set" devant :

Set RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(what:=UtilisateurActuel, LookIn:=xlValues, lookat:=xlWhole)

Peut-être?

Bonjour Cousinhub,

Merci pour ta réponse :)

Tu définis :

Dim RechercheVisa As Range

Et tu recherches un "String"...

Effectivement

Avec RechercheVisa en "String" et Set devant, j'ai une erreur "Object requis" sur cette ligne concernant RechercheVisa :

Set RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(what:=UtilisateurActuel, LookIn:=xlValues, lookat:=xlWhole)

Sans le Set devant RechercheVisa, j'ai une erreur "Qualificateur incorrect" sur cette ligne concernant RechercheVisa également :

strAdresse = RechercheVisa.Address

J'ai donc tenté de récupérer l'adresse de mon .find comme ceci :

Sub ControlVisa()

Dim dl As Long
Dim RechercheVisa As String
Dim StatutVisa As String
Dim UtilisateurActuel As String
Dim Adresse As Range

dl = Sheets("BD").Range("M" & Rows.Count).End(xlUp).Row

UtilisateurActuel = UCase(Environ("username"))
RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(What:=UtilisateurActuel, LookIn:=xlValues, LookAt:=xlWhole)
Set Adresse = Sheets("BD").Range("M2:M" & dl).Find(What:=UtilisateurActuel, LookIn:=xlValues, LookAt:=xlWhole).Address
StatutVisa = Adresse.Offset(0, 1).Value

If Not RechercheVisa Is Nothing Then 'condition : s'il existe au moins une occurrence trouvée
    If StatutVisa = "Oui" Then Exit Sub
    If StatutVisa = "Non" Or StatutVisa = "" Then USF_Avertissement.Show
Else:
    With Sheets("BD")
        .Unprotect "mdp"
        .Range("M:" & dl + 1).Value = UCase(Environ("username"))
        .Protect "mdp"
    End With
End If

End Sub

mais ce coup-ci j'ai une incompatibilité de type sur cette ligne, toujours sur RechercheVisa :

If Not RechercheVisa Is Nothing Then 'condition : s'il existe au moins une occurrence trouvée

Re-,

Non, tu ne touches à rien d'autre...

Tu laisses ta variable en "Range", et tout fonctionne...

Re-,

Tu devrais également déplacer cette ligne :

StatutVisa = RechercheVisa.Offset(0, 1).Value

'juste après celle-ci :

If Not RechercheVisa Is Nothing Then 'condition : s'il existe au moins une occurrence trouvée

Malheureusement j'ai toujours l'erreur 91 du début, mais sur cette ligne maintenant :

Set Adresse = Sheets("BD").Range("M2:M" & dl).Find(What:=UtilisateurActuel, LookIn:=xlValues, LookAt:=xlWhole).Address

Code complet :

Sub ControlVisa()

Dim dl As Long
Dim RechercheVisa As Range
Dim StatutVisa As String
Dim UtilisateurActuel As String
Dim Adresse As Range

dl = Sheets("BD").Range("M" & Rows.Count).End(xlUp).Row

UtilisateurActuel = UCase(Environ("username"))
Set RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(What:=UtilisateurActuel, LookIn:=xlValues, LookAt:=xlWhole)
Set Adresse = Sheets("BD").Range("M2:M" & dl).Find(What:=UtilisateurActuel, LookIn:=xlValues, LookAt:=xlWhole).Address
StatutVisa = Adresse.Offset(0, 1).Value

If Not RechercheVisa Is Nothing Then 'condition : s'il existe au moins une occurrence trouvée
    If StatutVisa = "Oui" Then Exit Sub
    If StatutVisa = "Non" Or StatutVisa = "" Then USF_Avertissement.Show
Else:
    With Sheets("BD")
        .Unprotect "mdp"
        .Range("M:" & dl + 1).Value = UCase(Environ("username"))
        .Protect "mdp"
    End With
End If

End Sub

Bon,

On va faire plus court :

Sub ControlVisa()

Dim dl As Long
Dim RechercheVisa As Range
Dim StatutVisa As String
Dim UtilisateurActuel As String

dl = Sheets("BD").Range("M" & Rows.Count).End(xlUp).Row

UtilisateurActuel = UCase(Environ("username"))
Set RechercheVisa = Sheets("BD").Range("M2:M" & dl).Find(what:=UtilisateurActuel, LookIn:=xlValues, lookat:=xlWhole)

If Not RechercheVisa Is Nothing Then 'condition : s'il existe au moins une occurrence trouvée
    StatutVisa = RechercheVisa.Offset(0, 1).Value
    If StatutVisa = "Oui" Then Exit Sub
    If StatutVisa = "Non" Or StatutVisa = "" Then USF_Avertissement.Show
Else:
    With Sheets("BD")
        .Unprotect "mdp"
        .Range("M:" & dl + 1).Value = UCase(Environ("username"))
        .Protect "mdp"
    End With
End If

End Sub

ça devrait le faire...

Oh super !

Merci beaucoup

Ca fonctionne parfaitement

Une très bonne journée à toi et au forum !

Re Cousinhub, le forum,

Je reviens car bien que je n'ai pas de message d'erreur, l'attribution des nouvelles valeurs ne sont font pas.

Je joins un fichier test pour plus de simplicité.

5test.xlsm (81.18 Ko)

Merci d'avance !

Dans mon fichier exemple, avec la MsgBox, on peut voir que ma variable StatutVisa est bien celle qu'elle devrait être, mais ne se renseigne pas dans la cellule de destination.

En remplaçant la variable "StatutVisa" par

RechercheVisa.Offset(0, 1).Value

cela fonctionne.

Pourtant j'ai bien défini

StatutVisa = RechercheVisa.Offset(0, 1).Value

EDIT : je viens de comprendre mon erreur de réflexion... Désolé !

Bonne journée !

Rechercher des sujets similaires à "erreur variable objet bloc definie"