Ersatz für eine if-Anweisung oder die Verwendung einer Sammlung
-
21-12-2019 - |
Frage
Vorhin habe ich eine Frage gestellt, auf die ich keine richtige Antwort bekommen konnte.Ich glaube, ich konnte mich nicht richtig ausdrücken.Dies ist ein Problem, mit dem ich konfrontiert bin:
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
Im letzten Fall (Fall ist = 3) gibt es a if-Anweisung.Das if-Anweisung ist eine Kurzversion (dies muss in Fall 0, Fall 1 und Fall 2 wiederholt werden).Ich muss etwas mit dem zurückgegebenen Wert machen (zum Beispiel) nxA_9N4.Bei einem früheren Versuch, ein Problem zu lösen, bekam ich Hilfe von:
Engineersmnky sagte er mir, ich solle eine Sammlung verwenden.
Ich habe eine Sammlung verwendet, aber trotzdem :(.
Kommentarantworten:-Wenn ein Zellenwert denselben Wert wie die Klasse hat (was immer der Fall ist), gebe ich den Wert von einem anderen Blatt zurück.Dieser Wert zählt die Gruppen/Klassen mit der Bezeichnung „xA4A“ und gibt den Zählwert als nxA4A zurück.Mit der if-Anweisung möchte ich wissen, ob class = „xA4A“ und mir dann die Anzahl von nxA4A geben.
Übrigens funktioniert die Auswertungsfunktion im Jahr 2013 nicht.
Lösung
Wenn man sich das anschaut Sammlung.Hinzufügen Sie werden sehen, dass diese Methode bis zu 4 Parameter annehmen kann.Der erste ist das hinzuzufügende Objekt (wie Sie es bereits tun), aber der zweite optionale Parameter ist der Schlüssel – ein eindeutiger Zeichenfolgenwert, der zur Identifizierung dieses Objekts in der Sammlung verwendet wird.
Wenn Sie also Elemente wie folgt zur Sammlung hinzufügen:
GroupenCol.Add nxA_9N4, "xA_9N4"
und Sie den erforderlichen String-Wert in einer Variablen namens „class“ haben, können Sie dann wie folgt auf die Sammlung zugreifen:
MsgBox GroupenCol(class)
oder dieses:
MsgBox GroupenCol.Item(class)
(Diese sind gleichwertig, weil Item
ist die Standardeigenschaft von a Collection
)