検索する日付を取得したり、検索する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