質問

以前、正しい答えが得られなかった質問をしました。自分の気持ちをうまく表現できなかったと思います。これは私が直面している問題です:

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

最後のケース (case is = 3) には、 if 文. 。これ if ステートメント これは短いバージョンです (これは、ケース 0、ケース 1、およびケース 2 で繰り返す必要があります)。戻り値を使って何かをする必要がある (例)nxA_9N4. 。以前に問題を解決しようとしたとき、私は次の助けを得ました。

エンジニアmnky 彼は私にコレクションを使うように言いました。

コレクションを使用しましたが、それでも:(。


コメントの回答:- cell.value がクラスと同じ値を持つ場合 (常にそうなります)、別のシートから値を返します。この値は、「xA4A」と呼ばれるグループ/クラスをカウントし、カウント値を nxA4A として返します。if ステートメントを使用して、class = "xA4A" であるかどうかを知り、nxA4A のカウントを返します。

ちなみに2013ではeval関数は動作しません。

役に立ちましたか?

解決

見てみると、 コレクション.追加 このメソッドでは、最大 4 つのパラメーターを受け取ることができることがわかります。最初のパラメーターは追加されるオブジェクトです (すでに行っているように) が、2 番目のオプションのパラメーターはキーです。コレクション内でこのオブジェクトを識別するために使用される一意の文字列値です。

したがって、次のようにコレクションに項目を追加すると、次のようになります。

GroupenCol.Add nxA_9N4, "xA_9N4"

「class」という変数に必要な文字列値がある場合は、次のようにコレクションにアクセスできます。

MsgBox GroupenCol(class)

またはこれ:

MsgBox GroupenCol.Item(class)

(これらは同等です。 Item のデフォルトのプロパティです Collection)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top