OVH Cloud OVH Cloud

Find

3 réponses
Avatar
news
Bonjour,

Comment, en VBA, savoir si une recherche à aboutie ou non et surtout ne pas
planter la SUB si l'ocurence n'as pas été trouvé ?

un exemple de code :

c = 12
debut:
Workbooks.Open ("G:\log\" & dat) 'dat contient le fichier à examiner
lot1 = lot & ".FINLOT" 'occurence à trouver
Cells.Find(what:=lot1).Activate
ActiveCell.Copy
Application.DisplayAlerts = False
ActiveWindow.Close
Application.DisplayAlerts = True
If Cells.Find(what:=lot1) Then
Feuil3.Range("A" & c).Select 'ou doit etre le resultat de la recherche
ActiveSheet.Paste
Else
Feuil3.Range("A" & c) = "Le fichier n'as pas été trouver dans " & dat & ""
MsgBox ("Voulez vous faire une autre recherche ?")
c = c + 1
UserForm2.Show 'userform2 sert à chosir le fichier à examiner
GoTo debut
End If

Le problème :

si l'occurence est trouvé tous de passe bien, mais autrement j'ai un message
d'erreur sur la ligne "Cells.Find(what:=lot1).Activate".

un grand merci, à qui me consacrera un peu de temps.

Alain

3 réponses

Avatar
AV
on error resume next
cells.find........
if err.number = 0 then ..... (occurrence trouvée)

AV
Avatar
JB
Bonjour,


PJ : http://cjoint.com/?bmj10YFmoN

A B
1 Nom Ville
2 Dupont Paris
3 Martin Lyon
4 Durand Paris

On cheche un nom:2 méthodes

1- Gestion d'erreur:

Sub cherche()
nomCherche = InputBox("Nom cherché? ")
On Error Resume Next
Err = 0
Range("A2:A14").Find(What:=nomCherche, LookIn:=xlValues).Select
If Err = 0 Then
Range(ActiveCell, ActiveCell.End(xlToRight)).Select
Else
MsgBox "Pas trouvé"
End If
On Error GoTo 0
End Sub

Test variable:

Sub cherche2()
nomCherche = InputBox("Nom cherché? ")
Set result = Range("A2:A14").Find(What:=nomCherche,
LookIn:=xlValues)
If result Is Nothing Then
MsgBox "Non trouvé"
Else
Range(result, result.End(xlToRight)).Select
End If
End Sub

Cordialement JB
Avatar
news
Un grand merci à vous deux

cela fonctione parfaitement

Bonne journée

Alain

"JB" a écrit dans le message de
news:
Bonjour,


PJ : http://cjoint.com/?bmj10YFmoN

A B
1 Nom Ville
2 Dupont Paris
3 Martin Lyon
4 Durand Paris

On cheche un nom:2 méthodes

1- Gestion d'erreur:

Sub cherche()
nomCherche = InputBox("Nom cherché? ")
On Error Resume Next
Err = 0
Range("A2:A14").Find(What:=nomCherche, LookIn:=xlValues).Select
If Err = 0 Then
Range(ActiveCell, ActiveCell.End(xlToRight)).Select
Else
MsgBox "Pas trouvé"
End If
On Error GoTo 0
End Sub

Test variable:

Sub cherche2()
nomCherche = InputBox("Nom cherché? ")
Set result = Range("A2:A14").Find(What:=nomCherche,
LookIn:=xlValues)
If result Is Nothing Then
MsgBox "Non trouvé"
Else
Range(result, result.End(xlToRight)).Select
End If
End Sub

Cordialement JB