Else If Statements

AK_Excel_13

New Member
Joined
Jul 11, 2014
Messages
30
Why doesn't my else if statement work? I seems like it should work. The error I keep getting is "Else without If"

Application.ScreenUpdating = False
Set x = ActiveCell
Count = Range("a65536").End(xlUp).Row


For e = 2 To Count
If Cells(e, 10).Value = "A" Then Cells(e, 10).Value = "Yes"
Else: If Cells(e, 10).Value = "B" Then Cells(e, 10).Value = "No"
End If
End If
Next e


x.Select
Application.ScreenUpdating = True
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If you only have two values "A" and "B" then this should suffice

Code:
If Cells(e, 10).Value = "A" Then
Cells(e, 10).Value = "Yes"
Else: Cells(e, 10).Value = "No"
End If
 
Upvote 0
this seems to work

Code:
If Cells(e, 10).Value = "A" Then
Cells(e, 10).Value = "Yes"
Else
If Cells(e, 10).Value = "B" Then
Cells(e, 10).Value = "No"
End If
End If
 
Upvote 0
Needs to be a bit clearer


also Count is a reserved word ( its a function so best not use it)

Code:
Application.ScreenUpdating = False
Set X = ActiveCell
MyCount = Cells(Rows.Count, "A").End(xlUp).Row

For e = 2 To MyCount
    If Cells(e, 10).Value = "A" Then
        Cells(e, 10).Value = "Yes"
    ElseIf Cells(e, 10).Value = "B" Then
            Cells(e, 10).Value = "No"
    End If
Next e

X.Select
Application.ScreenUpdating = True
 
Upvote 0
just as a note

when you put condition and action on one line

If Cells(e, 10).Value = "A" Then Cells(e, 10).Value = "Yes"

it imples an EndIf at the end ( to the compiler)

If Cells(e, 10).Value = "A" Then Cells(e, 10).Value = "Yes" EndIf


The professional approach is always to give a standard shape to conditions using the indent

Code:
If Cells(e, 10).Value = "A" Then 
    Cells(e, 10).Value = "Yes" 
EndIf[COLOR=#ff0000]
[/COLOR]


So other coders can easily see at a glance that it is a condition
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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