vba calculator - sub or function not defined

chiiz

New Member
Joined
Mar 10, 2009
Messages
3
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.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You have 2 End Function lines at the end - remove one - and you can also remove the
Code:
Private Sub UserForm_Click()
line at the start.
 
Upvote 0
You have 2 End Function lines at the end - remove one - and you can also remove the
Code:
Private Sub UserForm_Click()
line at the start.

I did that, but the problem remains the same. Now code looks like this:

Code:
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
 
Last edited by a moderator:
Upvote 0
You don't appear to have a cmdClear_Click sub.
 
Upvote 0
You don't appear to have a cmdClear_Click sub.

I don't really understand what I'm doing there, I had a page with codes that I wrote, exactly as there was, but I still get an error. I removed that cmdclear_click line and added :
Private Sub cmdClear_Click()
txtvar1.Value = ""
txtvar2.Value = ""
lblresult.Caption = ""
lbloperator.Caption = "+"
End Sub

</p:colorscheme>
Now I get error - Method or data member not found. It shows, that error is in this line - lblresult.Caption = vResult



</p:colorscheme>
 
Upvote 0
Do you have a label control called lblResult?
You might be better off asking these questions of your teacher so that he/she can explain and show you what is going on!
 
Upvote 0
hi,
i dont know if i can post my query on this thread as well because i am experiencing a similar problem or if i should start a new thread??

anyway i am experiecing the same error in my code on the line in bold
Code:
Sub getresults()
    Dim r As Integer                    'row counter
    Dim c As Integer                    'column counter
    Dim str As String                   'SQL string holder
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    With cn
        .ConnectionString = "Password=" & password & ";Persist Security Info=True;User ID=" & username & ";Data Source=" & DatabaseEnv & ";Mode=ReadWrite;"   'Assumption based on your connection string being valid
        .Provider = "IBMDADB2.DB2COPY1"      'assumption based on your provider being correct
        .Open                       'if the above are true then this will open DB
    End With
    For c = 31 To 35                 'columns AE to AI
        For r = 2 To 39                             'start with first row
            If Cells(r, c).Value <> "" Then            'is there a value in the row?
                str = Cells(r, c).Value                 'this is the SQL string we need for the recordset
                Cells(r, c - 5).Value = [B]getrecordset[/B](str) 'use a function to open the recordset and return the value to column 3
            End If
        Next r
    Next c                              'next column
End Sub
 
Upvote 0
You should post it as a new thread, please.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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