0

こんにちは、いくつかの条件に基づいて発生回数をカウントするマクロが必要です。データテーブルのサンプルは次のとおりです。

ID     Paydate        # of payments
1       5/1/2011            3
1       5/1/2011            3
1       3/1/2011            2
1       2/1/2011            1
2       6/12/2011           3
2       5/12/2011           2
2       4/12/2011           1
3       4/25/2011           2
3       3/25/2011           1

ID がその日付までに行った支払いの数をカウントしたい (3 番目の列が必要です)。たとえば、ID = 1 および paydate = 5/1/2011 の場合は 3 回の支払いがあり、ID=1 および paydate = 3/1/2011 の場合は 2 回の支払いがあります。マクロは、現在の日付以下の支払い回数をカウントする必要があり、同じ日付の ID が複数ある場合はカウントを増やしません。

素晴らしい数式でこれを行う方法がある場合は、複雑すぎるようです。どんな助けでも大歓迎です。

4

2 に答える 2

3

マクロや vba は必要ありません。

=COUNTIFS(A:A,A2,B:B,"<=" & B2)

これをC2に入れます。

于 2012-08-23T01:54:47.740 に答える
0

検索する日付を取得したり、検索するIDをどこで取得したりするかなど、続行するのに十分な情報がありません。したがって、一連の仮定を立てると、このような VBA を書くことができます。また、これは少し長いため、別の関数に分割してユーザーの応答を取得することもできます

Option Explicit
Sub numberOfPayments()
On Error Resume Next
Dim iRow As Integer
Dim iCol As Integer
Dim dtDate As Date
Dim iID As Integer
Dim sResponse As String
Dim iNumberOfPayments As Integer

'initialize the variables
iNumberOfPayments = 0
iRow = 2
iCol = 1

'get the date
sResponse = InputBox("Calculate for what date? (M/D/YYYY)", "Get Payment Date")

'check to make sure its a valid date
If IsDate(sResponse) Then
    'set date we are looking for
    dtDate = CDate(sResponse)
Else
    MsgBox "You must enter a valid date"
    Exit Sub
End If

'reset the response
sResponse = ""
'get the ID
sResponse = InputBox("Calculate for what ID?", "Get Payment ID")

'set the ID to look for
iID = CInt(sResponse)

'if the conversion failed there will be an error
If Err.Number <> 0 Then
    MsgBox "You must enter a valid ID"
    Exit Sub
End If

'assumes you have data in each cell
Do While Cells(iRow, iCol).Value <> ""
    'check the ID
    If Cells(iRow, iCol).Value = iID Then
        'this is done as not sure if the column is formatted as a date or just text
        If IsDate(Cells(iRow, iCol + 1).Text) Then
            If dtDate <= CDate(Cells(iRow, iCol + 1).Text) Then
                'add the payments
                iNumberOfPayments = iNumberOfPayments + Cells(iRow, iCol + 2).Value
            End If
        End If
    End If
    'move to the next row
    iRow = iRow + 1
Loop
'inform them of the number of payments
MsgBox "there are " + Str(iNumberOfPayments) + " total payments."

End Sub
于 2012-08-23T02:21:35.677 に答える