VBA或公式来基于日期分配金额

时间:2020-07-09 23:38:13

标签: excel vba excel-formula

是否可以根据日期分配(拆分)金额? 例如,我有这种情况:

ID           Date    Invoice value  ColumnD
ID0001    01/01/2020    200         here I need to insert the value
ID0001    01/02/2020    300         here I need to insert the value
ID0001    01/03/2020    500         here I need to insert the value
ID0002    01/03/2020    1000        here I need to insert the value
ID0002    01/04/2020    1500        here I need to insert the value

我知道要分配的值(对于ID0001是600,对于ID0002是2000)。 现在,我需要基于ID0001上的日期分配前100个。所以我可以:

ID           Date    Invoice value  ColumnD
ID0001    01/01/2020    200         0
ID0001    01/02/2020    300         0
ID0001    01/03/2020    500         400
ID0002    01/03/2020    1000        here I need to insert the value
ID0002    01/04/2020    1500        here I need to insert the value

第二个值(200)以此方式分配:

ID           Date    Invoice value  ColumnD
ID0001    01/01/2020    200         0
ID0001    01/02/2020    300         0
ID0001    01/03/2020    500         400
ID0002    01/03/2020    1000        0
ID0002    01/04/2020    1500        500

是否可以使用公式或VBA来做到这一点?

2 个答案:

答案 0 :(得分:0)

这里有一些VBA可以帮助您入门。它执行一些错误检查,但不是全部。最大的漏洞是如果付款额超过未结发票总额,该怎么办。 注意:我创建了一个范围名称来测试“ InvoiceID”,您将要使它成为一个动态范围名称,这样您以后就不必担心它了。该宏还假定发票号将始终按排序顺序!

Option Explicit

Sub PostInvoice()

   Dim zId  As Variant
   Dim dPmt As Variant
   Dim lRow As Long
   Dim rng  As Range
   
   lRow = 0
   
   Do While lRow = 0
   
     zId = InputBox("Enter Account ID", "Account Selection:")
     If (zId = "") Then Exit Sub
    
     On Error Resume Next
       lRow = Application.WorksheetFunction.Match(zId, Range("InvoiceIDs"), 0)
       Set rng = Cells(lRow, 4)
     On Error GoTo 0
   
     If (lRow = 0) Then
       MsgBox "Invoice ID: " & zId & " Not Found!", vbOKOnly + vbCritical
     End If
  
   Loop
   
   dPmt = InputBox("Enter Payment Amount", "Record a Payment:")
   If (dPmt = "") Then Exit Sub
   
     
   Do While dPmt > 0
     
     If (rng.Value > 0) Then
     
       If (dPmt >= rng.Value) Then
         dPmt = dPmt - rng.Value
         rng.Value = 0
       Else
         rng.Value = rng.Value - dPmt
         dPmt = 0
       End If
        
     End If
     
     Set rng = rng.Offset(1, 0)
     
     If (rng.Offset(0, -3).Value <> zId) Then Exit Do
   
   Loop

End Sub 'PostInvoice

要考虑的其他事情是将InputBoxes转换为UserForm,以实现更流畅的界面。

运行前:

enter image description here

运行后:

enter image description here HTH

答案 1 :(得分:0)

让“分配的值”放在D列中

然后在E2中将公式向下复制:

=MAX(0,SUMIF(A$2:A2,A2,C$2:C2)-SUMIF(A:A,A2,D:D))

enter image description here

相关问题