Aide:Prob. dans mon code VBA

Vinvinsylvain

XLDnaute Junior
Bonjour a tous!!!!!!


Voici mon code qui permet de recherche

Dim countTot As Long
Dim counter As Long
Dim strSearchString As String
Dim Ws As Object
Dim foundCell As Variant
Dim loopAddr As Variant
Dim returnValue As String
Dim strSearchString1 As String
Dim MyCollection As String

strSearchString = TxtNomVol.Value
strSearchString1 = TxtPrénomVol.Value

If strSearchString1 = '' Then strSearchString = '' Else


For Each Ws In Worksheets
countTot = countTot + Application.CountIf(Ws.UsedRange, '=' & strSearchString)
Next Ws
If countTot = 0 Then
returnValue = MsgBox(' Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' n'est pas connue de nos fichiers ', vbOKOnly, ' Message ')
Else
counter = 0
For Each Ws In Worksheets
With Ws
.Activate
Set foundCell = .Cells.Find(What:=strSearchString, LookIn:=xlValues, LookAt:=xlPart)
If Not foundCell Is Nothing Then
loopAddr = foundCell.Address
Do
counter = counter + 1
foundCell.Activate
If countTot = 2 Then
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' EST CONNUE DE NOS SERVICE, veuillez continué le constat de vol et intérroger le Récap. Interpelle ', vbOKOnly, ' Message ')
Exit Sub
End If
If counter = countTot Then
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' sélectionnée est la dernière !', vbOKOnly, 'Message')
Exit Sub
Else
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' a été ' & countTot & vbLf)
If returnValue = vbNo Then Exit For
Set foundCell = .Cells.FindNext(After:=foundCell)
End If
Loop While Not foundCell Is Nothing And foundCell.Address <> loopAddr
End If
End With
Next Ws
End If




Mon probleme s'est qu'il recherche la valeur (TxtNomVol et TxtPrénomVol) dans tous le classeur, j'aimerai qu'il recherche seulement dans la feuille RéCap. comment je peut fair.
Merci de Votre aide
 

Hervé

XLDnaute Barbatruc
re vinvin, le forum

Ton souci vient surement du fait que tu as inclus ta macro dans une boucle for each ws in worksheet.

Ce qui oblige à la macro d'appliquer ton code à toutes les feuilles, il faut surement supprimer cette boucle.

Ce qui devrait être fait dans le code suivant :

Dim countTot As Long
Dim counter As Long
Dim strSearchString As String
Dim Ws As Worksheet 'modif ici**************
Dim foundCell As Variant
Dim loopAddr As Variant
Dim returnValue As String
Dim strSearchString1 As String
Dim MyCollection As String

strSearchString = TxtNomVol.Value
strSearchString1 = TxtPrénomVol.Value

If strSearchString1 = '' Then strSearchString = '' Else


For Each Ws In Worksheets
countTot = countTot + Application.CountIf(Ws.UsedRange, '=' & strSearchString)
Next Ws
If countTot = 0 Then
returnValue = MsgBox(' Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' n'est pas connue de nos fichiers ', vbOKOnly, ' Message ')
Else
counter = 0
'For Each Ws In Worksheets'modif ici ********************
Set Ws = Sheets('RéCap')
With Ws
.Activate
Set foundCell = .Cells.Find(What:=strSearchString, LookIn:=xlValues, LookAt:=xlPart)
If Not foundCell Is Nothing Then
loopAddr = foundCell.Address
Do
counter = counter + 1
foundCell.Activate
If countTot = 2 Then
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' EST CONNUE DE NOS SERVICE, veuillez continué le constat de vol et intérroger le Récap. Interpelle ', vbOKOnly, ' Message ')
Exit Sub
End If
If counter = countTot Then
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' sélectionnée est la dernière !', vbOKOnly, 'Message')
Exit Sub
Else
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' a été ' & countTot & vbLf)
If returnValue = vbNo Then Exit For
Set foundCell = .Cells.FindNext(After:=foundCell)
End If
Loop While Not foundCell Is Nothing And foundCell.Address <> loopAddr
End If
End With
'Next Ws modif Ici**************
End If


PAr contre, je n'ai pas testé, si tu pouvais, en cas de nouveau souci, joindre un fichier exemple ca serait plus facile.

salut
 
M

Mathieu

Guest
ThisWorkbook.Worksheets('Nom_de_la_Feuille')

J'ai pas eu la patience de tout lire, mais tu n'as pas 'activé' cette macro pour une seule feuille. Avec la fonction ci-dessus ta macro sera seulement active dans la feuille où tu voudras.

Profites de regarder l'aide d'excel sur cette fonction :)
 

Vinvinsylvain

XLDnaute Junior
Non je ne parvient pas, j'ai rectifier encore le code mais il ne recherche que par le Nom je ne comprend plus rien!!!
voici le code:
Dim countTot As Long
Dim counter As Long
Dim strSearchString As String
Dim Ws As Object
Dim foundCell As Variant
Dim loopAddr As Variant
Dim returnValue As String
Dim strSearchString1 As String
Dim Ref As String

strSearchString = TxtNomVol.Value
strSearchString1 = TxtPrénomVol.Value

If strSearchString = '' Then strSearchString = '' Else
Ref = strSearchString '& strSearchString1

For Each Ws In Worksheets
countTot = countTot + Application.CountIf(Ws.UsedRange, '=' & Ref)
Next Ws
If countTot = 0 Then
returnValue = MsgBox(' Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' n'est pas connue de nos fichiers ', vbOKOnly, ' Message ')
Else
counter = 0
For Each Ws In Worksheets

With Ws
.Activate
Set foundCell = .Cells.Find(What:=strSearchString, LookIn:=xlValues, LookAt:=xlPart)
If Not foundCell Is Nothing Then
loopAddr = foundCell.Address
Do
counter = counter + 1
foundCell.Activate
If countTot = 1 Then
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' EST CONNUE DE NOS SERVICE, veuillez continué le constat de vol et intérroger le Récap. Interpelle ', vbOKOnly, ' Message ')
Exit Sub
End If
If counter = countTot Then
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' sélectionnée est la dernière !', vbOKOnly, 'Message')
Exit Sub
Else
returnValue = MsgBox('Cette Personne : ' & strSearchString & ' ' & strSearchString1 & ' a été ' & countTot & vbLf)
If returnValue = vbNo Then Exit For
Set foundCell = .Cells.FindNext(After:=foundCell)
End If
Loop While Not foundCell Is Nothing And foundCell.Address <> loopAddr
End If
End With
Next Ws
End If
 

Vinvinsylvain

XLDnaute Junior
ok s'est envoyer ouvre le dossier, clique sur constat, un formulaire s'ouvre remplie le nom et le prénom puis clique sur editer constat, tu arrive alors sur la liste inventaire clique sur fermer et là un message dis si cette personne est connue ou pas mais mon probleme s'est qu'il recherche dans tous le classeur, moi je veut uniquement dans la feuille 'Récap. Interpelle', le fameu code se trouve dans le VBA UserFormConstat puis dans le bouton Editer
 

Statistiques des forums

Discussions
312 239
Messages
2 086 508
Membres
103 238
dernier inscrit
ds776001