Excel Gestion Error

Résolu/Fermé
amine69500 Messages postés 418 Date d'inscription lundi 8 juin 2009 Statut Membre Dernière intervention 25 août 2013 - Modifié par amine69500 le 6/09/2012 à 11:09
amine69500 Messages postés 418 Date d'inscription lundi 8 juin 2009 Statut Membre Dernière intervention 25 août 2013 - 6 sept. 2012 à 15:31
Bonjour,


voila j'ai un code qui me permet de realiser une recherche dans un ficher CSV mais le probleme est que parfois il ne trouve rien, donc il me retourne une erreur,

mon but est lorsque ya une erreur, il m'affiche un msgbox qui me dis "ya une erreur"

et sort du code

merci de votre aide

voici mon code avec ce que j'ai essai mais il me l'affiche a chaque fois meme quand ya pas d'erreur


merci de votre aide


Code

Private Sub CommandButton1_Click()

Dim fich, qq

qq = Range("D14").Value
lqq = Len(qq)
'Chemin a adapter par vous
fic = "D:\Utilisateurs\temp\Desktop\testtoto.CSV"

Close
Open fic For Input As #1
Do While Not EOF(1)
'lecture enregistremnt
Line Input #1, buffer
'Recherche chaine test
If InStr(1, buffer, qq) Then

'ecriture mot devant chaine test
y = Right(buffer, Len(buffer) - Len(qq) - 1)


On Error GoTo xxx
xxx:

MsgBox "ya une erreur"

Fin:

Exit Sub


Debug.Print y
'sortie
Exit Sub
End If
Loop
Close



End Sub



Merci de votre aide

Cordialement
A voir également:

3 réponses

eljojo_e Messages postés 1155 Date d'inscription lundi 10 mai 2010 Statut Membre Dernière intervention 14 octobre 2022 152
Modifié par eljojo_e le 6/09/2012 à 13:02
Bonjour,

Private Sub CommandButton1_Click()
on error goto a:

Dim fich, qq

qq = Range("D14").Value
lqq = Len(qq)
'Chemin a adapter par vous
fic = "D:\Utilisateurs\temp\Desktop\testtoto.CSV"

Close
Open fic For Input As #1
Do While Not EOF(1)
'lecture enregistremnt
Line Input #1, buffer
'Recherche chaine test
If InStr(1, buffer, qq) Then

'ecriture mot devant chaine test
y = Right(buffer, Len(buffer) - Len(qq) - 1)

On Error GoTo xxx
xxx:

MsgBox "ya une erreur"

Fin:

Exit Sub

Debug.Print y
'sortie
Exit Sub
End If
Loop
Close

exit sub
a:
msgbox("Erreur")
End Sub

Cordialement,
0
f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 1 701
Modifié par f894009 le 6/09/2012 à 13:42
Re,
Une autre facon de voir, le fait de ne pas trouver n'est pas une erreur.

Private Sub CommandButton1_Click()
  Dim fich, qq

  qq = Range("D14").Value
  If qq = "" Then
    MsgBox "Pas de texte a chercher!!!!"
    Exit Sub
  End If
  lqq = Len(qq)
  'Chemin a adapter par vous
  fic = "D:\Utilisateurs\temp\Desktop\testtoto.CSV"

  Close
  
  Open fic For Input As #1
    Do While Not EOF(1)
      'lecture enregistremnt
      Line Input #1, buffer
      'Recherche chaine test
      If InStr(1, buffer, qq) Then

        'ecriture mot devant chaine test
        y = Right(buffer, Len(buffer) - Len(qq) - 1)
        Exit Sub
      End If
    Loop
      MsgBox "Pas trouve " & qq
  Close
End Sub
0
amine69500 Messages postés 418 Date d'inscription lundi 8 juin 2009 Statut Membre Dernière intervention 25 août 2013 12
6 sept. 2012 à 15:31
merci pour votre aide merci beaucoup,

les deux solutions marche super

merci encore

Cordialement
0