I'm trying to get both addition and subtraction options to work and display the proper symbol between each label box. I've been running into an error, which VBA highlights for me. The boxes A, B, and C need to be valued at 10, 11, and 12. When you click a box it's supposed to populate the first empty box with the value. When both boxes are full and you click another letter, its should not be changed, instead the values will be locked until you hit clear.
Current code:
Private Sub Addition_Click()
Me.Result = (Me.LblFirstNum + 0) + (Me.LblSecondNum + 0)
'lblresult = Val(LblFirstNum) + Val(LblSecondNum)
End Sub
Private Sub BtnA_Click()
With LblFirstNum
'to display text
.Caption = "10"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub BtnB_Click()
With LblSecondNum
'to display text
.Caption = "11"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub BtnC_Click()
With LblSecondNum
'to display text
.Caption = "12"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub Calculate_Click()
'Me.Result = (Me.LblFirstNum + 0) + (Me.LblSecondNum + 0)
lblresult.Value = Val(LblFirstNum.Value) + Val(LblSecondNum.Value)
End Sub
Private Sub Clear_Click()
Unload UserForm1
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
'Exit Command
UserForm1.Hide
End Sub
Private Sub Result_Change()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub LblFirstNum_Click()
End Sub
Private Sub LblSecondNum_Click()
End Sub
Private Sub LblSign_Click()
End Sub
Private Sub Subtraction_Click()
'Me.Result = (Me.LblFirstNum + 0) - (Me.LblSecondNum + 0)
lblresult.Value = Val(LblFirstNum.Value) - Val(LblSecondNum.Value)
End Sub
In Calculate_Click()
and Subtraction_Click()
, you've tried to retrieve the text shown on a label using LblFirstNum.Value
and LblSecondNum.Value
. Labels don't have a "Value" property. The correct property to use is "Caption".
In Addition_Click()
, it's unclear whether Result
is the correct name for the control you're trying to reference. Elsewhere in this code, you have lblresult
so you should establish which is the correct name and use that throughout.
You may also want to check the use of Unload in Clear_Click()
(assuming that UserForm1
is the form we are working with). Your current code will probably result in an automation error on the UserForm1.Show
line