Several mathematic formulas in VBA Userform

Vegarlo

New Member
Joined
Feb 23, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi.
I have an assigment making a calculator with different steps
userform.jpg


The first one "spenningskalkulator" is "standalone" and you only need one input to calculator the other 2, visa versa. That one is OK
The second one "Domenekalkulator" is also "standalone" and if you write in the box "grader" you cant write in the box "periode" and "frekvens" and is should pop up "NaN" (that one I havent figured out, getting NaN in"
The last one "Øyeblikksspenning" uses answers from "spenningskalkulator" and "domenekalkulator" to calculate the answers.
Ok the question. I have in the last button a formula that: Label17 = Round(vpeak.Text * Sin(WorksheetFunction.Pi * tbfrekvens * tbtidspunkt), 5) & " Volt"
The thing is that is doesnt add the number from the "tidspunkt"box.
Question 2; if you only write a number in "grader" (means angle) you can not use "periode" and "frekvens"box, and a different formula should be used: "Vpeak.text*sin(tbgrader.text)" but it doesnt know how to choose formulas and I only get a error.
here is the code (jeeezez this is a long post, parden my english)
VBA Code:
Private Sub CommandButton1_Click()

Dim V_peak, v_pp, v_rms As Double                  'variabler som får tall'
Dim input_teller As Integer
Dim ctrl As Control
v_pp = 0
V_peak = 0
v_rms = 0
input_teller = 0                         'inputtene blir 0 når du starter på nytt'

For Each ctrl In Me.Controls

If TypeName(ctrl) = "TextBox" Then        'sier at hvis du finner tekstbokser så gjør følgende'
        If Not ctrl.Text = "" Then       'sier at hvis IKKE er tom så..'
       
            Select Case ctrl.Name                                         'velg tekstboks med det navnet og sjekk om det står ett tall der'
            Case "vpp"
            If IsNumeric(ctrl.Object) Then v_pp = CDec(vpp.Text) Else MsgBox ("Du må skrive inn ett tall") 'er det tall der skal det skrives til tekstboksen og vpp feks får tallet'
            input_teller = input_teller + 1
            Case "vpeak"
            If IsNumeric(ctrl.Object) Then V_peak = CDec(vpeak.Text) Else MsgBox ("Du må skrive inn ett tall")
            input_teller = input_teller + 1
            Case "vrms"
            If IsNumeric(ctrl.Object) Then v_rms = CDec(vrms.Text) Else MsgBox ("Du må skrive inn ett tall")
            input_teller = input_teller + 1
            End Select
        End If
End If
Next
Select Case True                           'hvis boksene har verdi gjør utregning'
Case v_pp = 0 And v_rms = 0
     v_pp = V_peak * 2
     v_rms = V_peak * 0.7
    
Case v_pp = 0 And V_peak = 0
     v_pp = v_rms * 1 / Sqr(2)
     V_peak = v_pp / 2
    
Case v_rms = 0 And V_peak = 0
     v_rms = (v_pp / 2) * 1 / Sqr(2)
     V_peak = v_pp / 2
    
End Select

vpp.Text = Round(v_pp, 2)
vpeak.Text = Round(V_peak, 2)          'skrive svar til tekstboksene og 2desimaler'
vrms.Text = Round(v_rms, 2)

End Sub

Private Sub CommandButton2_Click()

Dim periode, frekvens, grader As Double


Dim input_teller As Integer
Dim ctrl As Control
grader = 0
periode = 0
frekvens = 0
input_teller = 0                         'inputtene blir 0 når du starter på nytt'

For Each ctrl In Me.Controls

If TypeName(ctrl) = "TextBox" Then        'sier at hvis du finner tekstbokser så gjør følgende'
        If Not ctrl.Text = "" Then       'sier at hvis IKKE er tom så..'
       
            Select Case ctrl.Name                                         'velg tekstboks med det navnet og sjekk om det står ett tall der'
            
             'If Not tbgrader.Text = "" Then 'skal si at hvis grader ikke er tom så gå hopp over og gå til ferdig'
            'GoTo ferdig
             Case "tbperiode"
             If IsNumeric(ctrl.Object) Then periode = CDec(tbperiode.Text) Else MsgBox ("Du må skrive inn ett tall") 'er det tall der skal det skrives til tekstboksen og vpp feks får tallet'
             input_teller = input_teller + 1
             Case "tbfrekvens"
             If IsNumeric(ctrl.Object) Then frekvens = CDec(tbfrekvens.Text) Else MsgBox ("Du må skrive inn ett tall")
             input_teller = input_teller + 1
             Case "tbgrader"
             If IsNumeric(ctrl.Object) Then grader = CDec(tbgrader.Text) Else MsgBox ("Du må skrive inn ett tall")
             input_teller = input_teller + 1
             GoTo ferdig
          
            End Select
            
           
        End If
End If

Next

Select Case True                           'hvis boksene har verdi gjør utregning'
Case frekvens = 0
     frekvens = 1 / periode
     'frekvens = NaN
Case periode = 0
     periode = 1 / frekvens
     'periode = NaN
Case grader = grader
'     periode = NaN
'     frekvens = NaN
    
End Select
       
ferdig:
tbperiode = Round(periode, 5)
tbfrekvens = Round(frekvens, 5)
tbgrader = Round(grader, 2)
  






End Sub

Private Sub CommandButton3_Click()
Dim tbfrekvens, tbperiode, V_peak, tbtidspunkt As Double
tbfrekvens = 0
tbperiode = 0
V_peak = 0
tbtidspunkt = 0


label16 = vpeak.Text & "V" & " * Sin(360 grader *" & tbfrekvens & "Hz * " & tbtidspunkt & "s) "
'label16 = vpeak.Text & "V" & " * Sin(tbgrader.text)"
Label17 = Round(vpeak.Text * Sin(WorksheetFunction.Pi * tbfrekvens * tbtidspunkt), 5) & " Volt"
'If Not tbtidspunkt.Text = "" Then
'label16 = Round(vpeak.Text * Sin(WorksheetFunction.Pi * tbfrekvens.Text * tbperiode.Text * tbtidspunkt.Text), 2) & " Volt"
'End If
End Sub
 
Last edited by a moderator:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
See if this example helps:

VBA Code:
Private Sub CommandButton3_Click()
Dim v
If Len(Me.Tbgrader) And Not Len(Me.Tbfrekvens) And Not Len(Me.Tidspunkt) Then
    Me.Label1 = Me.vpeak * Sin(Me.Tbgrader)
Else
    v = Sin(WorksheetFunction.Pi * Me.Tbfrekvens * Me.Tidspunkt)
    Me.Label1 = Round(Me.vpeak * v, 5) & " Volt"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,575
Members
452,652
Latest member
eduedu

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