For X = 1 To UBound(arTemp) Debug.Print "-------> trouvé Ligne : " & arTemp(X) ' * Ici mettre le code pour la concaténation If Cells(arTemp(X), "C").Value = ValCherchee Then Cells(arTemp(X), "C").Font.Bold = True Z = X Do While Cells(arTemp(Z) + 1, "C").Font.Bold = False concat = concat & "," & Chr(10) & Cells(arTemp(Z) + 1, "C").Value Cells(ligneCell, "B").Value = Right(concat, Len(concat) - 1) Cells(ligneCell, "B").Select Z = Z + 1 Loop End If Next
Pourquoi Z = Z + 1 est égal à Faux?
Je ne comprend pas pourquoi?
Dim z As Integer
z = 2
MsgBox z + 1 ' affichera bien 3
MsgBox z = z + 1 ' affichera faux car z ne saurait être égal à z + 1
Function FindAll(ByVal sText As String, ByRef oSht As Worksheet, ByRef sRange As String, ByRef arMatches() As String) As Boolean
' --------------------------------------------------------------------------------------------------------------
' FindAll - To find all instances of the1 given string and return the row numbers.
' If there are not any matches the function will return false
' --------------------------------------------------------------------------------------------------------------
On Error GoTo Err_Trap
Dim rFnd As Range ' Range Object
Dim iArr As Integer ' Counter for Array
Dim rFirstAddress ' Address of the First Find
' -----------------
' Clear the Array
' -----------------
Erase arMatches
Set rFnd = oSht.Range(sRange).Find(what:=sText, LookIn:=xlValues, lookAt:=xlPart)
If Not rFnd Is Nothing Then
rFirstAddress = rFnd.Address
Do Until rFnd Is Nothing
iArr = iArr + 1
ReDim Preserve arMatches(iArr)
arMatches(iArr) = rFnd.Row 'rFnd.Address pour adresse complete ' rFnd.Row Pour N° de ligne
Set rFnd = oSht.Range(sRange).FindNext(rFnd)
If rFnd.Address = rFirstAddress Then Exit Do ' Do not allow wrapped search
Loop
FindAll = True
Else
' ----------------------
' No Value is Found
' ----------------------
FindAll = False
End If
' -----------------------
' Error Handling
' -----------------------
Err_Trap:
If Err <> 0 Then
MsgBox Err.Number & " " & Err.Description, vbInformation, "Find All"
Err.Clear
FindAll = False
Exit Function
End If
End Function
Sub Conc()
'------------------------------------------------------
' Tableau contenant la liste des lignes trouvées
' par la méthode findAll()
'------------------------------------------------------
Dim arTemp() As String
'------------------------------------------------------
' Autres variables :
'------------------------------------------------------
Dim nomF As String
Dim n As Integer
nomF = "CODE_DTR"
Dim PlageRecherche As String
PlageRecherche = "C2:C30000"
Dim MaRange As Range
Set MaRange = Range("A2:A30000")
Dim concat As String
Dim Z As Integer
'Dim cellulleCourante As Integer
'------------------------------------------------------
' Début du traitement :
'------------------------------------------------------
'On parcourt la colonne A
For Each cell In MaRange
ValCherchee = cell.Value
ligneCell = cell.Row
concat = ""
If ValCherchee <> "" Then
'---------------------------------------------------------------
bFound = FindAll(ValCherchee, Sheets(nomF), PlageRecherche, arTemp())
'---------------------------------------------------------------
Debug.Print "Ligne: " & ligneCell & " : " & ValCherchee & " ---> " & bFound
If bFound = True Then
Debug.Print "Nb occurences : " & UBound(arTemp)
For X = 1 To UBound(arTemp)
Debug.Print "-------> trouvé Ligne : " & arTemp(X)
' * Ici mettre le code pour la concaténation
If Cells(arTemp(X), "C").Value = ValCherchee Then
Z = arTemp(X)
Cells(Z, "C").Font.Bold = True
Cells(Z, "C").Select
'Do While celCourante.Value.Font.Bold = False
Do While Cells(Z + 1, "C").Font.Bold = False
concat = concat & "," & Chr(10) & Cells(Z + 1, "C").Value
Cells(ligneCell, "B").Value = Right(concat, Len(concat) - 1)
Cells(ligneCell, "B").Select
Z = Z + 1
Loop
End If
Next
End If
End If
Next
End Sub
Sub test()
Dim Z As Integer
Y = 2
MsgBox Y + 1 ' affichera bien 3
MsgBox Y = Y + 1 ' affichera faux car z ne saurait être égal à z + 1
End Sub
25 nov. 2014 à 09:00
Merci pour votre aide.
C'est ce je constatais la boucle faisait du surplace. J'ai approté les modifications et j'ai ce message d'erreur: "'Erreur d'exécution 9': L'indice n'appartient à la séléction'" au second passage sur la ligne de code:
Je pense y'a erreur ailleurs dans la boucle do while mais je n'arrive à comprendre.
Je vous remercie d'avance.
25 nov. 2014 à 09:14
essaye avec X=0 to ubound (arTemp)-1
Modifié par sokpassy85 le 25/11/2014 à 09:32
Au fait je pense que c'est l'arTemp() qui pose problème, sa valeur reste figer donc la variable associée (Z) ne pourra pas s'incrémenter. Est il possible de faire incrémenter l'arTemp()?
Ci dessous le code total
25 nov. 2014 à 09:33
Qu'il faut laisser.
Hein ?????
- Que vaut Z au moment du crash ?
- Que vaut UBound(arTemp) ?
- Que vaut X au moment du crash ?
- Que contient ton tableau arTemp (issue je suppose d'un FINDALL..^^ ) ?
Modifié par sokpassy85 le 25/11/2014 à 09:49
- Que vaut Z au moment du crash : 2 mais Z=Z+1 = faux
- Que vaut UBound(arTemp) : 1
- Que vaut X au moment du crash : 1
- Que contient ton tableau arTemp (issue je suppose d'un FINDALL..^^ ) : L'indice n'appartient pas à la sélection oui il est issu d'un FINDALL.