pour le remplacement d'une instruction if ou l'utilisation d'une collection
-
21-12-2019 - |
Question
Plus tôt, j'ai posé une question où je ne pouvais pas obtenir une réponse correcte.Je pense que je n'arrivais pas à m'exprimer correctement.C'est un problème, je suis confronté:
Sub organize()
Dim GroupID(1 To 30) As String
Dim GroupAanwezig As Long
Dim class As String
Dim i, j, t, totally, countCol As Integer
Dim GroupenCol As New Collection
i = 1
t = 1
rn_totallist = Worksheets("totallist").Cells.SpecialCells(xlCellTypeLastCell).Row
nxA_9N4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_9N4")
nxA_1A2A = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1A2A")
nxA_1A2B = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1A2B")
nxA_2A2 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2A2")
nxA_2A3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2A3")
nxA_3A3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3A3")
nxA_1B4A = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4A")
nxA_1B4B = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4B")
nxA_1B4C = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4C")
nxA_1B4D = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4D")
nxA_2G4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2G4")
nxA_3G4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3G4")
nxA_4G4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_4G4")
nxA_2A4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2A4")
nxA_3A4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3A4")
nxA_4A4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_4A4")
nxA_1E3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1E3")
nxA_1E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1E4")
nxA_2E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2E4")
nxA_3e3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3E3")
nxA_3e4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3E4")
nxA_4e4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_4E4")
totalstd = nxA_9N4 + nxA_1A2A + nxA_1A2B + nxA_2A2 + nxA_2A3 + nxA_3A3 + nxA_1B4A + nxA_1B4B + nxA_1B4C + nxA_1B4D + nxA_2G4 + nxA_3G4 + nxA_4G4 + nxA_2A4 + nxA_3A4 + nxA_4A4 + nxA_1E3 + nxA_1E4 + nxA_2E4 + nxA_3e3 + nxA_3e4 + nxA_4e4
GroupenCol.Add nxA_9N4
GroupenCol.Add nxA_1A2A
GroupenCol.Add nxA_1A2B
GroupenCol.Add nxA_2A2
GroupenCol.Add nxA_2A3
GroupenCol.Add nxA_3A3
GroupenCol.Add nxA_1B4A
GroupenCol.Add nxA_1B4B
GroupenCol.Add nxA_1B4C
GroupenCol.Add nxA_1B4D
GroupenCol.Add nxA_2G4
GroupenCol.Add nxA_3G4
GroupenCol.Add nxA_4G4
GroupenCol.Add nxA_2A4
GroupenCol.Add nxA_3A4
GroupenCol.Add nxA_4A4
GroupenCol.Add nxA_1E3
GroupenCol.Add nxA_1E4
GroupenCol.Add nxA_2E4
GroupenCol.Add nxA_3e3
GroupenCol.Add nxA_3e4
GroupenCol.Add nxA_4e4
LastRow = Sheets("BLS").Cells.SpecialCells(xlCellTypeLastCell).Row
For i = 2 To LastRow
LastCol = Sheets("BLS").Cells(i, Columns.count).End(xlToLeft).Column
For j = 2 To LastCol
class = Sheets("BLS").Cells(i, j).Value
GroupAanwezig = WorksheetFunction.CountIf(Range("B1:D18"), class)
BLSers = Sheets("BLS").Cells(i, 1).Value
If InStr(nshow, class) = 0 Then
GroupID(t) = class
nshow = nshow & " " & GroupAanwezig & " " & t & " " & GroupID(t) & vbCrLf
t = t + 1
End If
countCol = Sheets("BLS").Cells(i, Columns.count).End(xlToLeft).Column
lastcolumn = countCol - 1
Select Case lastcolumn
Case Is = 0
'MsgBox "EMPTY"
Case Is = 1
totally = lastcolumn * 30
Case Is = 2
totally = ((lastcolumn * 10) - 5) * 2
Case Is = 3
totally = lastcolumn * 10
nclass = "n" + class
MsgBox nclass
'---------------------------------------------------------
'there has to be a faster way to do the if-statement below.
'---------------------------------------------------------
If class = "xA_9N4" Then
MsgBox nxA_9N4
End If
If class = "xA_4A4" Then
MsgBox nxA_4A4
End If
If class = "xA_3A4" Then
MsgBox nxA_3A4
End If
If class = "xA_1B4B" Then
MsgBox nxA_1B4B
End If
If class = "xA_1A2B" Then
MsgBox nxA_1A2B
End If
If class = "xA_2A4" Then
MsgBox nxA_2A4
End If
If class = "xA_2E4" Then
MsgBox nxA_2E4
End If
If class = "xA_1E3" Then
MsgBox nxA_1E3
End If
'--------------------------------------------------------
End Select
End Sub
Dans le dernier cas (cas est = 3) il y a un si l'instruction.Cette si l'instruction est un shor version (cette opération doit être répétée en cas de 0, cas 1 et 2).J'ai besoin de faire quelque chose avec la valeur retournée (par exemple) nxA_9N4.Dans une tentative de résoudre un problème que j'ai reçu de l'aide de:
engineersmnky il m'a dit d'utiliser une collection.
J'ai utilisé une collection mais quand même :(.
Commentaire réponses:-si une cellule.la valeur est a la même valeur que la classe (qui est toujours le cas), alors je vais retourner la valeur à partir d'une autre feuille.Cette valeur compte le nombre de groupes/classes "xA4A" et renvoie la countvalue comme nxA4A.avec le ifstatement je voudrais savoir si class = "xA4A" alors donnez-moi le nombre de nxA4A.
Par la voie eval ne fonctionne pas en 2013.
La solution
Si vous regardez la Collection.Ajouter méthode, vous verrez qu'il peut prendre jusqu'à 4 paramètres.Le premier est l'objet à ajouter (comme vous le faites déjà), mais le second paramètre est la clé d'une unique chaîne de valeur utilisée pour identifier cet objet dans la collection.
Donc, si vous ajouter des éléments dans la collection comme ceci:
GroupenCol.Add nxA_9N4, "xA_9N4"
et que vous avez la chaîne de valeur dans une variable appelée "classe", vous pouvez ensuite accéder à la collection comme ceci:
MsgBox GroupenCol(class)
ou ceci:
MsgBox GroupenCol.Item(class)
(Les deux sont équivalents, car Item
est la propriété par défaut d'un Collection
)