我正在制作一个电子表格来控制我的财务状况。我制作了一个VBA代码,在其中我输入了4个不同的数据:I(客户名称;ii(合同总价值;iii(支付合同的月数;iv(首次付款日期。
我已经能够完成并使用代码,该代码在4个不同的列中用这些信息填充数据库。
问题是,我希望它(当我单击插入按钮时(自动分割价值,并根据将要支付的月数填写日期(分期付款(。例如:
用户表单
- 客户名称:John
- 总值:1000.00美元
- 付款次数:4
- 首次付款日期:2020年1月1日
也就是说,约翰将于2020年1月1日(1月(付给我250美元$250在2020年1月2日(2月(等等…我希望它在数据库上显示这样的内容:
客户端 | 值 | 日期-mm/dd/yyyy |
---|---|---|
Claire | 2000 | 2019年5月12日(数据库上的单个付款示例( |
John | 250 | 020年1月//tr>|
John | 250 | 020年1月2日//tr>|
John | 250 | 020年1月3日//tr>|
John | 250 | 020年1月4日//tr>|
标记 | 500 | 020年2月6日(示例(//tr>
您可以尝试此代码。
优化这一点始终是可能的。也许一个小错误修复是可能的。但如果没有文本数据表,那就不清楚了。
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim Name a string
Dim counter As Integer
Dim money As Double
Dim Date as Date
Dim i As Integer
Set ws = Worksheets("Projetos")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the database
With ws
.Cells(iRow, 2).Value = Me.boxCliente.Value 'Client
.Cells(iRow, 3) = CCur(boxValor.Value) 'Money
.Cells(iRow, 4).Value = Me.boxParcela.Value 'Number of Payments
.Cells(iRow, 5) = CDate(boxData.Value) 'Date
If .Cells(iRow, 4) <> 1 Then
Name = .Cells(iRow, 2).Value
counter = .Cells(iRow, 4).Value
money = .Cells(iRow, 3).Value
Date = .Cells(iRow, 5).Value
For i = 0 To counter - 1
.Cells(iRow + i, 2).Value = Name
.Cells(iRow + i, 3).Value = money / counter
.Cells(iRow + i, 5).Value = Format(DateAdd("m", i, Date), "mmddyyyy")
Next i
End With
'clear the data
Me.boxCliente.Value = ""
Me.boxValor.Value = ""
Me.boxParcela.Value = ""
Me.boxData.Value = ""
End Sub