Ok so here is my homework. We need to make a calculator in excel visual basic. Here is link. There are 2 calculators - "Calculator 1" and "Calculator 3 - Combobox". I need help with 3 - combobox. Code looks like this:
Private Sub UserForm_Click()
Option Explicit
Dim vResult As Variant
Private Sub UserForm_Initialize()
cbooperators.List() = Array("+", "-", "x", "/", "&")
cbooperators.ListIndex = 0
cmdClear_Click
End Sub
Private Sub cboOperators_Change()
lbloperator.Caption = cbooperators.Value
cmdCalculate_Click
End Sub
Private Sub cmdCalculate_Click()
Dim vVar1 As Variant
Dim vVar2 As Variant
vVar1 = txtvar1.Text
vVar2 = txtvar2.Text
Select Case lbloperator.Caption
Case "+"
vResult = Val(vVar1) + Val(vVar2)
Case "-"
vResult = vVar1 - vVar2
Case "x"
vResult = vVar1 * vVar2
Case "/"
vResult = vVar1 / vVar2
Case Else
MsgBox "No such case!!“"
End Select
lblresult.Caption = vResult
txtResultSubtype.Value = getSubtypeName()
End Sub
Function getSubtypeName()
Dim cDataType As String
Select Case VarType(vResult)
Case vbDouble
cDataType = "Double"
Case vbString
cDataType = "String"
Case Else
cDataType = Str(VarType(vResult))
End Select
getSubtypeName = cDataType
End Function
End Function
When I try to run it, I get an compile error - sub or function not defined. I am beginner in vba, so any help would be appreciated.
Private Sub UserForm_Click()
Option Explicit
Dim vResult As Variant
Private Sub UserForm_Initialize()
cbooperators.List() = Array("+", "-", "x", "/", "&")
cbooperators.ListIndex = 0
cmdClear_Click
End Sub
Private Sub cboOperators_Change()
lbloperator.Caption = cbooperators.Value
cmdCalculate_Click
End Sub
Private Sub cmdCalculate_Click()
Dim vVar1 As Variant
Dim vVar2 As Variant
vVar1 = txtvar1.Text
vVar2 = txtvar2.Text
Select Case lbloperator.Caption
Case "+"
vResult = Val(vVar1) + Val(vVar2)
Case "-"
vResult = vVar1 - vVar2
Case "x"
vResult = vVar1 * vVar2
Case "/"
vResult = vVar1 / vVar2
Case Else
MsgBox "No such case!!“"
End Select
lblresult.Caption = vResult
txtResultSubtype.Value = getSubtypeName()
End Sub
Function getSubtypeName()
Dim cDataType As String
Select Case VarType(vResult)
Case vbDouble
cDataType = "Double"
Case vbString
cDataType = "String"
Case Else
cDataType = Str(VarType(vResult))
End Select
getSubtypeName = cDataType
End Function
End Function
When I try to run it, I get an compile error - sub or function not defined. I am beginner in vba, so any help would be appreciated.