Need help finishing code Please!!

cblackst

New Member
Joined
Jan 6, 2005
Messages
39
Hello, Can anyone give me any suggestions.


I create a form using excels VB editor. What I have is 5 variable and 5 textboxes for the variables. The program is suppose to allow the user to enter a "TOT POPSIZE" and "ErrorSize", Than once the Calc button is press three results will display in three other textbox which would be a Condidence of 90 answer, Confidence of 95 answer and a Confidence of 99.

Now, my problem is the input textbox should never =0 if they do a MsgBox should appear with a return statement. Can someone help me finish the IF statement. BELOW is my sample code. I know not to sharp that's why I'm here for help!!




Private Sub CommandButton1_Click()
Dim PopSize, ErrSize, ConfUnc90, ConfUnc95, ConfUnc99 As Double

ErrSize = CDbl(txtbox1)
PopSize = CDbl(txtbox2)

If ErrSize = 0 Then

MsgBox ("Population Size should be > 0")


Else
PopSize = 0
MsgBox ("Population Size should be > 0")


Else
ErrSize = eval(form.ErrSize.Value)

form.PopSize.Value = "" + PopSize
form.ErrSize.Value = "" + ErrSize

ConfUnc90 = (0.680625 / ((ErrSize / 100) * (ErrSize / 100)))
ConfUnc95 = (0.9604 / ((ErrSize / 100) * (ErrSize / 100)))
ConfUnc99 = (1.658944 / ((ErrSize / 100) * (ErrSize / 100)))

ConfUnc90.Value = "" + Math.Round(ConfUnc90)
ConfUnc95.Value = "" + Math.Round(ConfUnc95)
ConfUnc99.Value = "" + Math.Round(ConfUnc99)


ConfSamp90.Value = "" + Math.Round((ConfUnc90 * PopSize) / (ConfUnc90 + PopSize))
ConfSamp95.Value = "" + Math.Round((ConfUnc95 * PopSize) / (ConfUnc95 + PopSize))
ConfSamp99.Value = "" + Math.Round((ConfUnc99 * PopSize) / (ConfUnc99 + PopSize))


End If
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

chiello

Well-known Member
Joined
Jan 18, 2005
Messages
848
Ciao cblackst
you have just to type

Exit Sub

after msgbox statement.

(y)
 

chiello

Well-known Member
Joined
Jan 18, 2005
Messages
848
:oops: :oops:

Private Sub CommandButton1_Click()
...

ErrSize = CDbl(txtbox1)
PopSize = CDbl(txtbox2)

If ErrSize = 0 Then

MsgBox ("Population Size should be > 0")
Exit Sub

Else
if PopSize = 0 Then
MsgBox ("Population Size should be > 0")
Exit Sub

Else
...
...
End If
End If
End Sub
 

cblackst

New Member
Joined
Jan 6, 2005
Messages
39

ADVERTISEMENT

Thank all. two more Questions.

So is this right than? and if so what line do I need to use to start my calculation section, after this.

Private Sub CommandButton1_Click()
Dim PopSize, ErrSize, ConfUnc90, ConfUnc95, ConfUnc99 As Double

ErrSize = CDbl(txtbox1.Text)
PopSize = CDbl(txtbox2.Text)

If ErrSize = 0 Then

MsgBox ("Population Size should be > 0")


Else

If PopSize = 0 Then

MsgBox ("Population Size should be > 0")

Exit Sub
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()

<SPAN style="color:#00007F">Dim</SPAN> PopSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, ErrSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> ConfUnc90 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, ConfUnc95 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, ConfUnc99 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>

ErrSize = <SPAN style="color:#00007F">CDbl</SPAN>(txtbox1.Text)
PopSize = <SPAN style="color:#00007F">CDbl</SPAN>(txtbox2.Text)

<SPAN style="color:#00007F">If</SPAN> ErrSize = 0 <SPAN style="color:#00007F">Then</SPAN>
    MsgBox ("Population Size should be > 0")
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
<SPAN style="color:#00007F">ElseIf</SPAN> PopSize = 0 <SPAN style="color:#00007F">Then</SPAN>
    MsgBox ("Population Size should be > 0")
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
<SPAN style="color:#00007F">Else</SPAN>
   <SPAN style="color:#007F00">' Do your stuff here....</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 

cblackst

New Member
Joined
Jan 6, 2005
Messages
39

ADVERTISEMENT

Hello All,

Can anybody tell me what wrong with this.??/ Thanks


Private Sub CommandButton1_Click()
Dim PopSize, ErrSize, ConfUnc90, ConfUnc95, ConfUnc99 As Double

ErrSize = CDbl(txtbox1.Text)
PopSize = CDbl(txtbox2.Text)

If ErrSize = 0 Then

MsgBox ("Population Size should be > 0")


Else

If PopSize = 0 Then

MsgBox ("Population Size should be > 0")

Else


PopSize.Value = "" + PopSize
ErrSize.Value = "" + ErrSize

ConfUnc90 = (0.680625 / ((ErrSize / 100) * (ErrSize / 100)))
ConfUnc95 = (0.9604 / ((ErrSize / 100) * (ErrSize / 100)))
ConfUnc99 = (1.658944 / ((ErrSize / 100) * (ErrSize / 100)))



TextBox3 = "" + Math.Round((ConfUnc90 * PopSize) / (ConfUnc90 + PopSize))
TextBox4 = "" + Math.Round((ConfUnc95 * PopSize) / (ConfUnc95 + PopSize))
TextBox5 = "" + Math.Round((ConfUnc99 * PopSize) / (ConfUnc99 + PopSize))

End If
End If
End Sub
 

Whisperer14

Well-known Member
Joined
Nov 6, 2002
Messages
589
One of your errors which has been admirably covered by just_jon is the declaration of variables.
Each of these must be complete, if there is no 'as Double', or whatever, then the variable will be a Variant type
Dim PopSize, ErrSize, ConfUnc90, ConfUnc95, ConfUnc99 As Double
should be
Dim PopSize As Double, ErrSize As Double
Dim ConfUnc90 As Double, ConfUnc95 As Double, ConfUnc99 As Double
(y)
 

cblackst

New Member
Joined
Jan 6, 2005
Messages
39
Thank You. for your help.

I have change my code, the one problem that I think is not correct now is where I need the results of ConfUnc90 ,ConfUnc95 & ConfUnc99 to display in their appropriate textboxes. Any suggestion on what I should change below.


Private Sub CommandButton1_Click()

Dim PopSize As Double, ErrSize As Double
Dim ConfUnc90 As Double, ConfUnc95 As Double, ConfUnc99 As Double

ErrSize = CDbl(txtbox1.Text)
PopSize = CDbl(txtbox2.Text)

If ErrSize = 0 Then
MsgBox ("Population Size should be > 0")
Exit Sub
ElseIf PopSize = 0 Then
MsgBox ("Population Size should be > 0")
Exit Sub

Else


ConfUnc90 = (0.680625 / ((ErrSize / 100) * (ErrSize / 100)))
ConfUnc95 = (0.9604 / ((ErrSize / 100) * (ErrSize / 100)))
ConfUnc99 = (1.658944 / ((ErrSize / 100) * (ErrSize / 100)))


TextBox3.Text = "" + Math.Round((ConfUnc90 * PopSize) / (ConfUnc90 + PopSize))
TextBox4.Text = "" + Math.Round((ConfUnc95 * PopSize) / (ConfUnc95 + PopSize))
TextBox5.Text = "" + Math.Round((ConfUnc99 * PopSize) / (ConfUnc99 + PopSize))





End If

End Sub
 

Forum statistics

Threads
1,147,665
Messages
5,742,502
Members
423,734
Latest member
123hmMission

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
Top