else without if error message

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Not sure what is wrong with the following cold. Thank you so much
+++
Sub grade()
Dim x As Integer
Dim y As Integer
Dim z As Integer
For x = 1 To 20
y = Workbooks(1).Worksheets(2).Cells(x, 1).Value
If y > 80 Then z = "a"
ElseIf y > 60 And y < 80 Then z = "b"
ElseIf y > 50 And y < 60 Then z = "c"
Else
z = "f"
End If
Workbooks(1).Worksheets(2).Cells(x, 3).Value = z
Next
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You haven't explained exactly what it is that you are trying to do, but I see an obvious error right off the bat:
Code:
[COLOR=#333333]Dim z As Integer[/COLOR]
Code:
[COLOR=#333333]If y > 80 Then z = "a"[/COLOR]
a, b, c, and f are not Integers.
You need to declare z as String, not Integer.
 
Upvote 0
You're assigning a string to an integer and your ElseIf statements are not correct.

Code:
Sub grade()

Dim x As Integer
Dim y As Integer
Dim z As String

For x = 1 To 20
    y = Workbooks(1).Worksheets(2).Cells(x, 1).Value
    If y >= 80 Then
        z = "a"
    ElseIf y >= 60 Then
        z = "b"
    ElseIf y >= 50 Then
        z = "c"
    Else
        z = "f"
    End If
    Workbooks(1).Worksheets(2).Cells(x, 3).Value = z
Next

End Sub

WBD
 
Upvote 0
Thank you. I changed z to string type but still the same error. the code suppose to check A1 to A20 cells value. if the value of A1 for example is less than 50 then cell C1 should say f.
If A1 is 90 then C1 is A etc.
 
Upvote 0
Did you see WBD's response?
He identified other issues with your code, and offered a solution.
 
Upvote 0
Thank you. It is working now. So beside the String issue. Does it really matters if I type

++++++++++++++++
ElseIf y > 60 And y < 80 Then z = "b"

Or it should be

ElseIf y > 60 And y < 80 Then
z = "b"
+++++++++++++++

because the first one did not work. So I think I can not type anything after "Then" in the same line.

Thank you
 
Upvote 0
Correct. Your original solution also didn't cater for a value of exactly 80. I also omitted checking for "< 80" since it's already handle in the first if statement.

WBD
 
Upvote 0
You could simplify that greatly by using Select Case instead of all the If/ElseIf. I also added a condition for the value being blank:

Code:
Sub grade()

Dim x As Integer
Dim z As String

For x = 1 To 20
    Select Case Workbooks(1).Worksheets(2).Cells(x, 1).Value
        Case Is > 80: z = "A"
        Case Is >= 60: z = "B"
        Case Is >= 50: z = "C"
        Case "": z = ""
        Case Else: z = "F"
    End Select
    Workbooks(1).Worksheets(2).Cells(x, 3).Value = z
Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

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