问题
I'm making a spreadsheet to control my finances. I made a VBA code where I enter 4 different data: i) Client's name; ii) Total value of the contract; iii) Number of months that the contract will be paid; iv) Date of the first payment.
I've been able to complete and use the code, that fills a database with these information in 4 different columns.
The thing is, I want it to (when I click the insert button) automatically split the value and fill the dates according to the number of months that it will be paid (payment in installments). For example:
USERFORM
- Client name: John
- Total value: $ 1,000.00
- Number of payments: 4
- Date of the first payment: 01/01/2020
That said, John will pay me $250 on 01/01/2020 (january); $250 on 02/01/2020 (february) and so on... and I want it to show on the data base something like this:
Client | Value | Date - mm/dd/yyyy |
---|---|---|
Claire | 2,000 | 12/05/2019 (example of a single payment on database) |
John | 250 | 01/01/2020 |
John | 250 | 02/01/2020 |
John | 250 | 03/01/2020 |
John | 250 | 04/01/2020 |
Mark | 500 | 06/02/2020 (example) |
And I don't have a clue how to do this... Can someone help me?
The code is:
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
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
.Cells(iRow, 3) = CCur(boxValor.Value)
.Cells(iRow, 4).Value = Me.boxParcela.Value
.Cells(iRow, 5) = CDate(boxData.Value)
End With
'clear the data
Me.boxCliente.Value = ""
Me.boxValor.Value = ""
Me.boxParcela.Value = ""
Me.boxData.Value = ""
End Sub
回答1:
You can try this code.
Its always possible to optimize this. Maybe a little bugfix its possible. But without a text datasheet its not clear.
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
来源:https://stackoverflow.com/questions/65297823/use-excel-vba-to-fill-database-with-payments-split-in-months