• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Add values in a form

Hi,

I'm creating an input form in Excel using VBA

one field contains contract_admin_costs

another field contains contract_staff_costs


I want a third field to add contract_admin_costs and contract_staff_costs and input the answer in a third field as soon as the two other fields are input by the user, I know this is really easy in excel but how do I programme a field in VBA to do this?
 
Kevinonearth


Can you upload a sample file so we can see what your doing and possibly assist you?

Refer: http://chandoo.org/forums/topic/posting-a-sample-workbook
 
Sure, it is the Officer Form

when a user inputs staff costs and then admin costs I want the admin plus staff costs field to add and fill automatically


staff costs: 1000

admin costs: 500

staff plus admin: 1500 (adds and fills automatically)


https://dl.dropboxusercontent.com/u/90990975/LTI%20Input%20form.xlsm
 
Try this:

[pre]
Code:
Private Sub Text_Contract_Admin_AfterUpdate()
Me.Text_Admin_Plus_Staff.Value = CDbl(Me.Text_Contract_Admin.Value) + CDbl(Me.Text_Staff_Costs.Value)
End Sub

Private Sub Text_Staff_Costs_AfterUpdate()
Me.Text_Admin_Plus_Staff.Value = CDbl(Me.Text_Contract_Admin.Value) + CDbl(Me.Text_Staff_Costs.Value)
End Sub

Private Sub UserForm_Activate()
Me.Text_Staff_Costs.Value = "0.00"
Me.Text_Contract_Admin.Value = "0.00"

End Sub
[/pre]
 
Try this:

[pre]
Code:
Private Sub Text_Contract_Admin_AfterUpdate()
Me.Text_Admin_Plus_Staff.Value = CDbl(Me.Text_Contract_Admin.Value) + CDbl(Me.Text_Staff_Costs.Value)
End Sub

Private Sub Text_Staff_Costs_AfterUpdate()
Me.Text_Admin_Plus_Staff.Value = CDbl(Me.Text_Contract_Admin.Value) + CDbl(Me.Text_Staff_Costs.Value)
End Sub

Private Sub UserForm_Activate()
Me.Text_Staff_Costs.Value = "0.00"
Me.Text_Contract_Admin.Value = "0.00"

End Sub
[/pre]
 
Ok, thanks for reply, but what is CDbl ?

Would it be possible to put the code in the file and upload it back to dropbox again?
 
CDbl converts the text string to a double floating decimal number


On your userform, double click somewhere and paste the code in :)
 
Can I ask you also, if I were to create this form in Access would the code be the same?

Are the rules for VBA the same in Excel and Access?
 
Unless it is pulling any data from sheets I imagine it should be ok. If you pull data from sheets you will need to create a table and refer to that
 
Back
Top