2

A列に名前があり、B列に次のような数字があります。

jimmy 4
jimmy 4
carl  8
john  8

ジミーの数を合計する必要があります。つまり、A列に同じ値がいくつかある場合、その名前のB番号の合計です。だからジミー= 8.どうすればいいですか?私はvbaが初めてなので、私にとって簡単なことはそれほど簡単ではありません:)

編集、マクロ:

Sub Sample()
    Dim path As String
    Dim openWb As Workbook
    Dim openWs As Worksheet
    Dim DataInizio As String
    Dim DataFine As String

    path = "C:\Me\Desktop\example.xls"

    Set thiswb = ThisWorkbook

    Set openWb = Workbooks.Open(path)
    Set openWs = openWb.Sheets("details")

    Set Logore = thiswb.Sheets("Log")

    With openWs
         start = CDate(InputBox("start (gg/mm/aaaa)"))
         end = CDate(InputBox("end (gg/mm/aaaa)"))
         Sheets("details").Select

         LR = Cells(Rows.Count, "A").End(xlUp).Row
         dRow = 2

         For r = 2 To LR
         If Cells(r, 1) >= start And Cells(r, 1) <= end Then
         ' Do un nome alle colonne nel file di log indicandone la posizione
           ore = Range("K" & r)
           nome = Range("J" & r)
           totore = totore + ore
              If ore <> 8 Then
                Range("A" & r & ",J" & r & ",D" & r & ",K" & r).Copy Logore.Cells(dRow, 1)
                rigatot = dRow
                dRow = dRow + 1
              End If
              If nome <> Range("J" & r + 1) Then
                  If totore <> 40 Then
                    Logore.Cells(dRow, 5) = totore
                  End If
                  totore = 0
              End If
          End If
         Next
         thiswb.Sheets("Log").Activate
    End With
    openWb.Close (False)
End Sub
4

1 に答える 1

3

さて、このマクロは値を合計し、それらを新しいリストとして再出力します。メインサブで列を文字列パラメーターとして指定できます。


CollectArray "A", "D"-列から配列を収集しA、重複を削除してから、列に出力しますD

DoSum "D", "E", "A", "B"- 列のすべての値を合計し、Dそれらを列に書き込みますE- 列からの一致と列からのA値を取得しますB


前:

ここに画像の説明を入力

Option Explicit

Sub Main()

    CollectArray "A", "D"

    DoSum "D", "E", "A", "B"

End Sub


' collect array from a specific column and print it to a new one without duplicates
' params:
'           fromColumn - this is the column you need to remove duplicates from
'           toColumn - this will reprint the array without the duplicates
Sub CollectArray(fromColumn As String, toColumn As String)

    ReDim arr(0) As String

    Dim i As Long
    For i = 1 To Range(fromColumn & Rows.Count).End(xlUp).Row
        arr(UBound(arr)) = Range(fromColumn & i)
        ReDim Preserve arr(UBound(arr) + 1)
    Next i
    ReDim Preserve arr(UBound(arr) - 1)
    RemoveDuplicate arr
    Range(toColumn & "1:" & toColumn & Range(toColumn & Rows.Count).End(xlUp).Row).ClearContents
    For i = LBound(arr) To UBound(arr)
        Range(toColumn & i + 1) = arr(i)
    Next i
End Sub


' sums up values from one column against the other column
' params:
'           fromColumn - this is the column with string to match against
'           toColumn - this is where the SUM will be printed to
'           originalColumn - this is the original column including duplicate
'           valueColumn - this is the column with the values to sum
Private Sub DoSum(fromColumn As String, toColumn As String, originalColumn As String, valueColumn As String)
    Range(toColumn & "1:" & toColumn & Range(toColumn & Rows.Count).End(xlUp).Row).ClearContents
    Dim i As Long
    For i = 1 To Range(fromColumn & Rows.Count).End(xlUp).Row
        Range(toColumn & i) = WorksheetFunction.SumIf(Range(originalColumn & ":" & originalColumn), Range(fromColumn & i), Range(valueColumn & ":" & valueColumn))
    Next i
End Sub


Private Sub RemoveDuplicate(ByRef StringArray() As String)
    Dim lowBound$, UpBound&, A&, B&, cur&, tempArray() As String
    If (Not StringArray) = True Then Exit Sub
    lowBound = LBound(StringArray): UpBound = UBound(StringArray)
    ReDim tempArray(lowBound To UpBound)
    cur = lowBound: tempArray(cur) = StringArray(lowBound)
    For A = lowBound + 1 To UpBound
        For B = lowBound To cur
            If LenB(tempArray(B)) = LenB(StringArray(A)) Then
                If InStrB(1, StringArray(A), tempArray(B), vbBinaryCompare) = 1 Then Exit For
            End If
        Next B
        If B > cur Then cur = B
        tempArray(cur) = StringArray(A)
    Next A
    ReDim Preserve tempArray(lowBound To cur): StringArray = tempArray
End Sub

後:

ここに画像の説明を入力

于 2013-10-21T10:05:12.483 に答える