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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
: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
 
Upvote 0
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
 
Upvote 0
<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>
 
Upvote 0
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
 
Upvote 0
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)
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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