Déclarer une variable qui soit du même type qu'une autre

Bonjour,

J'aimerais qu'une variable b soit déclarée du même type qu'une autre variable a.

Dim a as String
Dim b as VarType(a)

Mais ce code ne marche pas.

En fait j'en ai besoin dans cette fonction, qui me supprime tous les doublons d'un array.

Function remove_duplicates(array_remove_in)

    'Delete all duplicates in an array
    'Return the same array without duplicates

    Dim i As Integer
    Dim array_without_duplicates() As Variant
    Dim j_count As Integer

    ReDim array_without_duplicates(0) As Variant
    'Of course, the first element will be in the new array
    array_without_duplicates(0) = array_remove_in(LBound(array_remove_in))

    For i = LBound(array_remove_in) + 1 To UBound(array_remove_in)

        If Not is_this_element(array_without_duplicates, array_remove_in(i)) Then
            'If an element has not been already copied in array_without_duplicates

            j_count = j_count + 1
            ReDim Preserve array_without_duplicates(j_count) 'New row
            array_without_duplicates(j_count) = array_remove_in(i)

        End If

    Next i
   remove_duplicates = array_without_duplicates

End Function

Cette fonction s'appuie sur celle-ci, qui regarde si un élément est présent dans une array.

Function is_this_element(array_look_in, research_look_for)

    'https://www.excel-pratique.com/fr/astuces_vba/recherche-tableau-array.php
    'Return True if research_look_for (as single variable) is in table_look_in (as an array)

    Dim i As Integer
    is_this_element = False

    For i = LBound(array_look_in) To UBound(array_look_in)
        If array_look_in(i) = research_look_for Then
            is_this_element = True
            Exit For
        End If
    Next

End Function

Comme on peut le voir je suis obligé de créer une variable Dim array_without_duplicates() As Variant ce qui ne m'arrange pas ! En effet cette fonction sert pour des array d'integer, de string, de long, et du coup ces variables doivent aussi être déclarées en Variant sinon bug.

Savez-vous comment faire ?

Bonjour,

Bonjour,

J'aimerais qu'une variable b soit déclarée du même type qu'une autre variable a.

Dim a as String
Dim b as VarType(a)

Mais ce code ne marche pas.

Il faudrait faire un select case sur ceci :

0    Empty (non initialisée) 
1    Null (aucune donnée valide) 
2    Entier 
3    Entier long 
4    Nombre à virgule flottante en simple précision 
5    Nombre à virgule flottante en double précision 
6    Valeur monétaire 
7    Valeur de date 
8    Chaîne 
9    Objet 
10    Valeur d'erreur 
11    Valeur booléenne 

si pas trouvé => Variant !

Rechercher des sujets similaires à "declarer variable qui soit meme type"