Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
#20489
Boa tarde

criei esta função para me retornar todos valores com um critério porém preciso eliminar os repetidos.

Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)

Dim i As Long
Dim result As String

If Search_string = vbNullString Then
result = ""
Lookup_concat = Trim(result)
End
End If


For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
result = result & "/" & Return_val_col.Cells(i, 1).Value
End If
Next

result = Right(result, Len(result) - 1)
Lookup_concat = Trim(result)

End Function

ela funciona me trazendo todos valores em uma celula somente com o critério desejado porem não elimina os repetidos.
Imagem
#20494
Olá, Leandro.

Experimente substituir este trecho
Código: Selecionar todos
For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
result = result & "/" & Return_val_col.Cells(i, 1).Value
End If
Next

por este
Código: Selecionar todos
For i = 1 To Search_in_col.Count
 If Search_in_col.Cells(i, 1) = Search_string Then
  If Application.CountIf(Range(Search_in_col.Cells(1, 1), Search_in_col.Cells(i, 1)), Search_string) = 1 Then
   result = result & "/" & Return_val_col.Cells(i, 1).Value
  End If
 End If
Next
Se não resolver disponibilize a sua planilha e não imagem dela.
#20496
Tente isso.

Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)

Dim i As Long
Dim result As String
Dim unico As New Collection

If Search_string = vbNullString Then
result = ""
Lookup_concat = Trim(result)
End
End If

On Error Resume Next
For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
unico.Add Return_val_col.Cells(i, 1).Value, Return_val_col.Cells(i, 1).Value & "a"
End If
Next
On Error GoTo 0

For i = 1 To unico.Count
result = result & "/" & unico.Item(i)
Next

result = Right(result, Len(result) - 1)
Lookup_concat = Trim(result)

End Function
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord