Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

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
Bonjour vinvin, le forum



j'ai pas de solution à ton souci, mais je suis resté sur ma faim sur ce post :

Ce lien n'existe plus

Donc, si tu pouvais me dire ou on en est de cette aventure, ca me ferait plaisir tout plein.

salut
 

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
 
M

Mathieu

Guest
Euh je me sens très très confus, j'ai enregistré et fermé le fichier excel... et à ma grande surprise il ne fonctionne plus :-/

Je me bas pour essayer de le faire remarcher, je te tiens au courant.
 
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…