Subtract the numeric values of 2 Textboxes

Denny57

Active Member
Joined
Nov 23, 2015
Messages
259
Office Version
  1. 365
Platform
  1. Windows
I am trying to populate a text box in a user form with the result of the subtraction of the values of 2 other text boxes.

Similar code works when the format of the textboxes are currency

VBA Code:
Private Sub EndReadingDay_AfterUpdate()

    Dim c As Double, d As Double
    c = txtStartReadingDay.Value
    d = txtEndReadingDay.Value
    
    txtUnitsUsedDay.Value = Format(Val(d - c, "#,##0.0"))
       
End Sub

NOTE: the Textboxes have a maximum of 6 characters and the values are to one decimal place

Thank you in advance
 
Try changing this line:
VBA Code:
    txtUnitsUsedDay.Value = Format(Val(d - c, "#,##0.0"))
to this:
VBA Code:
    txtUnitsUsedDay.Value = Format(Val(d) - Val(c), "#,##0.0")
 
Upvote 0
I'm not sure about the names of your textboxes, but try this, just check the names of the textboxes and your event EndReadingDay_AfterUpdate

VBA Code:
Private Sub EndReadingDay_AfterUpdate()
    Dim c As Double, d As Double
    
    If txtStartReadingDay.Value <> "" Then
      c = CDbl(txtStartReadingDay.Value)
    End If
    If txtEndReadingDay.Value <> "" Then
      d = txtEndReadingDay.Value
    End If
    
    txtUnitsUsedDay.Value = Format(d - c, "#,##0.0")
       
End Sub
 
Upvote 0
Solution
Unfortunately both solutions do not complete the required action. The details are meter readings both to 1 decimal place.
I was hoping to populate the difference between the start and end textbox values into the txtUnitsUsedDay textbox.

By using AfterUpdate (as I have used previously and which works in other areas of the full code as well as in other files) I was trusting the difference to be automatically populated into the txtUnitsUsedDay textbox as soon as both the other textboxes are populated.

This is not happening and I am confused why this mighe be the case.
 
Upvote 0
Unfortunately both solutions do not complete the required action.
...
I'm not sure about the names of your textboxes
Apparently the names of your textboxes are:

txtStartReadingDay and txtEndReadingDay


the txtUnitsUsedDay textbox as soon as both the other textboxes are populated.

According to the above, the event must be Change.

Replace your code with the following:

VBA Code:
Private Sub txtEndReadingDay_Change()
  Call Procedure_update_txtUnitsUsedDay
End Sub

Private Sub txtStartReadingDay_Change()
  Call Procedure_update_txtUnitsUsedDay
End Sub

Private Sub Procedure_update_txtUnitsUsedDay()
  Dim c As Double, d As Double
 
  If txtStartReadingDay.Value <> "" Then
    c = CDbl(txtStartReadingDay.Value)
  End If
  If txtEndReadingDay.Value <> "" Then
    d = CDbl(txtEndReadingDay.Value)
  End If

  txtUnitsUsedDay.Value = Format(d - c, "#,##0.0")
End Sub

In the future, it would be ideal if you put all your code, that way we help you with a better solution. Otherwise, we will assume that with a simple idea, you will be able to adapt it to your code.

🧙‍♂️
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,797
Messages
6,193,055
Members
453,772
Latest member
aastupin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top