2017-01-12 1 views
0

je ligne:tableaux Uniqe contient deux valeurs

enter image description here

Je veux ajouter cette valeur à l'onglet/tableaux, pour obtenir des paires comme ci-dessous:

[0] = Eng, 3

[1] = PL, 4

[2] = US, 5 ...

[x] = valeur, col_number

S'il vous plaît me dire comment:

1) ajouter la deuxième valeur

2) vérifier si les tableaux contient "Eng" ou "PL"

jusqu'à présent, j'ont:

Dim CatTab() As Variant 
Dim tabSize as Long 
For b = 3 To 20 
    Category = wsSum.Cells(2, b).Value 
    tabSize = tabSize + 1 
    ReDim Preserve CatTab(tabSize) 
    CatTab(tabSize) = Category 
Next 
+0

Vous pouvez déclarer un tableau de 2 dimensions, par exemple 'Dim CatTab (4,4)'. Ne pensez pas que vous avez besoin de Redim car vous semblez connaître la taille à l'avance. – SJR

Répondre

4

Je vous recommande d'utiliser un deux faible tableau ensional à la place pour stocker des valeurs séparées.

Dim CatTab As Variant 
'First dimension can't be redimmed, use this for headers. Second dimension can, use this to extend data. 
ReDim CatTab(1 To 2, 1 To 1) 

Dim tabSize As Long 
For b = 3 To 20 
    'Check if a resize is required 
    If CatTab(1, UBound(CatTab, 2)) <> "" Then ReDim Preserve CatTab(1 To UBound(CatTab, 1), 1 To UBound(CatTab, 2) + 1) 
    'Add value 
    CatTab(1, UBound(CatTab, 2)) = wssum.Cells(2, b) 
    'Add column 
    CatTab(2, UBound(CatTab, 2)) = b 
Next 

Pour itérer pour vérifier une valeur spécifique ..

Dim lng As Long 
For lng = LBound(CatTab, 2) To UBound(CatTab, 2) 
    If InStr(1, CatTab(1, lng), "Eng") <> 0 Or InStr(1, CatTab(1, lng), "PL") <> 0 Then 
     'code here 
    End If 
Next lng 
+0

merci de votre aide @Zerk! – 4est