0

私はVBAは初めてですが、改善するために本を読んでいます。現在、列「A」から列を取得し、それらを識別子として使用して、別の列で IF ELSEIF ステートメントを実行しています。

基本的にrange(A1:A3)、値「ERIC」は各セル [A1 = ERIC、A2 = ERIC...] に存在し、range(B1:B3)3 つの異なる整数値 [B1 = 2、B2 = 9...] になります。範囲「ERIC」のこれらの整数の大きい方を見つけ、範囲「ERIC」の最大値をセル(C1)に入れる必要があります。

range(A4:A6)次に、 integer range(B4:B6) [B4 = 1, B5 = 4...] に対応する値 "Sally"のプロセスを繰り返します。最大値は cell(C4) に入ります。約 40 個の名前があります。

助けてください。ありがとう。

4

1 に答える 1

2

これは、あなたが尋ねるようにする必要があります。Sheet 1これは、あなたが にいて、あなたの名前が にありColumn A、値がにあると仮定しますColumn B

         Public Sub FindNameAndGreatestValue()
    Dim nameColumnRowCount As Integer
    Dim nameColumn As Integer
    Dim valueColumn As Integer
    Dim outputColumn As Integer
    Dim currentName As String

    nameColumnRowCount = Cells(Rows.Count, 1).End(xlUp).Row
    currentName = ""
    nameColumn = 1     '1 = A - change this to column that has names
    valueColumn = 4    '4 = D - change this to the column that has values
    outputColumn = 5   '5 = E - change this to column that should contain output
    Dim currentLargestForName As Integer
    Dim currentNameStartRow As Integer
    currentLargestForName = -999
    currentName = Cells(1, nameColumn).Value
    currentNameStartRow = 1

    Dim currentRow As Integer
    For currentRow = nameColumn To nameColumnRowCount + 1
        'if last known name is the same as the current row's name
        If StrComp(currentName, Cells(currentRow, nameColumn).Value, vbTextCompare) = 0 Then
            'if current rows number is larger than the last known largest number
            If currentLargestForName < CInt(Cells(currentRow, valueColumn).Value) Then
                currentLargestForName = CInt(Cells(currentRow, valueColumn).Value)
            End If
        Else
            'drop into here if the names no longer match, meaning a new name was found.
            'output the largest known number from the previous name into the first row of that name
            Cells(currentNameStartRow, outputColumn).Value = currentLargestForName
            currentNameStartRow = currentRow    'save the row this new name starts at for number output later
            currentLargestForName = CInt(Cells(currentRow, valueColumn).Value)
            currentName = Cells(currentRow, nameColumn).Value
        End If
    Next
End Sub

ここに画像の説明を入力

ここに画像の説明を入力

于 2013-03-12T21:19:01.613 に答える