是否可以根据日期分配(拆分)金额? 例如,我有这种情况:
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来做到这一点?
答案 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,以实现更流畅的界面。
运行前:
运行后:
答案 1 :(得分:0)