English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone help me with my code? It keeps showing Pass and Merit for every name and option button i select.


Option Explicit
Dim Average_Mark As Double

Private Sub CmdOK1_Click()
If FrmMark.OptModule1 = True Then
Range("Data!c26").Value = 5
End If
If FrmMark.OptModule2 = True Then
Range("Data!c26").Value = 9
End If
If FrmMark.OptModule3 = True Then
Range("Data!c26").Value = 13
End If

Average_Mark = Range("Data!c28").Value

If Average_Mark <= 40 Then
MsgBox "Fail"
End If
If Average_Mark > 40 < 55 Then
MsgBox "Pass"
End If
If Average_Mark >= 55 < 70 Then
MsgBox "Merit"
End If
If Average_Mark >= 70 Then
MsgBox "Distinction"
End If

End Sub

2007-12-12 03:21:05 · 5 answers · asked by basics_87 1 in Computers & Internet Programming & Design

Ok thanks guys i got that part to work but i still have one problem.

On the form i have a list of names, but the message keeps displaying the message for one name.

I want it so i can click on a name and it show the answers for the person.....

...If im getting too complicated dont worry. Thanks.

2007-12-12 04:06:38 · update #1

5 answers

I think your second set of if statements is set up wrong. Mainly for the Pass and Merit conditions. I think it should look like:
If Average_Mark <= 40 Then
...
ElseIf Average_Mark > 40 And Average Mark < 55 Then
...
ElseIf Average_Mark >= 55 And Average_Mark < 70 Then
...
ElseIf Average_Mark >= 70 Then
...
End If

And to answer the question above me, it is VBA (Visual Basic for Applications) The language Excel uses to creates its macros.

2007-12-12 03:28:45 · answer #1 · answered by Mike 2 · 0 0

Correct me if I'm wrong but the first half of the procedure has nothing to do with the second half. The first half sets the value of cell C26; however, when you set the value of Average_Mark it refers to cell C28.

I think that's your problem.

Also instead of using multiple If statements, use a Select Case Statement.

Select Case Average_Mark
Case <=40
MsgBox "Fail"
Case <55
MsgBox "Pass"
Case <70
MsgBox "Merit"
Case Else
MsgBox "Distinction"
End Select

This statement does exactly what you had but it much cleaner and more efficient as the code doesn't have to go thru each if statement that you have.

Now I admit, I get confused as to which one is greater than or lesser than. So it is entirely possible you need to switch them.

2007-12-12 11:41:39 · answer #2 · answered by AJ 7 · 0 0

Your syntax for these two subs is wrong
If Average_Mark > 40 < 55 Then
MsgBox "Pass"
End If
If Average_Mark >= 55 < 70 Then
MsgBox "Merit"
End If

they should be:
If Average_Mark > 40 and Average_Mark < 55 Then

and

If Average_Mark >54 and Average_Mark < 70 Then

Looking at your code this should resolve your issue...

2007-12-12 11:44:18 · answer #3 · answered by newton3010 6 · 0 0

What mike said!!

Not sure if it works in VBA.. but in VB you can use a case statment.

Example:


Select case Average_Mark
case <= 40
Msgbox("fail")
case 41 to 54
Msgbox("pass")
case 55 to 69
msgbox....
Case else
msgbox...
End select

2007-12-12 11:35:56 · answer #4 · answered by vstar_in_texas 3 · 0 0

What language is that

2007-12-12 11:28:22 · answer #5 · answered by Anonymous · 0 0

fedest.com, questions and answers