Question

J'écris un extrait pour exclure les cellules dupliquées.Donc, s'il y a une cellule dans la colonne 4 qui commence par 0-9 (tout en format texte) et n'apparaît pas dans la colonne 8, j'aimerais ajouter la valeur de la cellule à une variable de chaîne.Mais il semble que la méthode d'intersection ne renvoie toujours rien ici ... une idée?

Sub getAddNum()

addnum = ""

Set rng1 = Columns(4).SpecialCells(xlCellTypeConstants)
Set rng2 = Columns(8).SpecialCells(xlCellTypeConstants)

For Each currentcell In rng1
    cellValue = CStr(currentcell.Value)
    If InStr("0123456789", CStr(Left(cellValue, 1))) And Intersect(currentcell, rng2)        Is Nothing Then
    addnum = addnum & CStr(currentcell.Value) & ", "
End If
Next 

Range("I9").Value = addnum

End Sub

Était-ce utile?

La solution

Comme je l'ai commenté, réécrivez votre code comme celui-ci:

Sub getAddNum()

Dim addnum As String, cellValue As String
Dim rng1 As Range, rng2 As Range, currentcell As Range

addnum = ""

Set rng1 = Columns(4).SpecialCells(xlCellTypeConstants)
Set rng2 = Columns(8).SpecialCells(xlCellTypeConstants)

For Each currentcell In rng1
    cellValue = CStr(currentcell.Value)
    If Left(cellValue, 1) Like "[0-9]" And IsError(Application.Match(cellvalue,rng2,0)) Then
    addnum = addnum & CStr(currentcell.Value) & ", "
End If
Next 

Range("I9").Value = addnum

End Sub

Non testé, mais je pense que vous pouvez obtenir la logique.
J'espère que cela aide en quelque sorte.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top