Else without if error

shukhar

Board Regular
Joined
Jun 1, 2007
Messages
62
Hi

I'm trying to use the code below:

If Abs(Sheets("Template").Range("C39")) = 0 Then Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = "" _

Else: If Application.IsText(Sheets("Template").Range("d14")) Then MsgBox "Error in price calculation", vbOKOnly, "error"
Else: Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = Sheets("Template").Range("D14").Value

End If
Next i

However, VBA does not seem to like it. I get the error message 'Else without if' on the second if satement. Not sure what is wrong as there is an if statement which proceeds the Else...

Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try entering your IF statement like this:
Code:
If Abs(Sheets("Template").Range("C39")) = 0 Then 
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = ""
Else 
    If Application.IsText(Sheets("Template").Range("d14")) Then 
        MsgBox "Error in price calculation", vbOKOnly, "error"
    Else 
        Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = Sheets("Template").Range("D14").Value
    End If
End If
 
Upvote 0
If your going to use both the Then and Else parts of the if, they cannot be on the same line as the condtion...
Code:
If Condition Then
    Do this if it's true
Else
    Do that if it's false
End if

Hope that helps..
 
Upvote 0
This will compile.
Code:
If Abs(Sheets("Template").Range("C39")) = 0 Then
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = "" 
ElseIf Application.IsText(Sheets("Template").Range("d14")) Then
    MsgBox "Error in price calculation", vbOKOnly, "error"
Else
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = Sheets("Template").Range("D14").Value
End If
But I don't know if it will actually do what you want because I'm not really following the logic of what you posted.:)
 
Last edited:
Upvote 0
Hi

Norie left in a line continuation character. Once removed it should work:

Code:
If Abs(Sheets("Template").Range("C39")) = 0 Then
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = "" 
ElseIf Application.IsText(Sheets("Template").Range("d14")) Then
    MsgBox "Error in price calculation", vbOKOnly, "error"
Else
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = Sheets("Template").Range("D14").Value
End If
 
Upvote 0
This will compile.
Code:
If Abs(Sheets("Template").Range("C39")) = 0 Then
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = "" 
ElseIf Application.IsText(Sheets("Template").Range("d14")) Then
    MsgBox "Error in price calculation", vbOKOnly, "error"
Else
    Sheets("Deals").Range("TraderStart").Offset(i, 0).Value = Sheets("Template").Range("D14").Value
End If
But I don't know if it will actually do what you want because I'm not really following the logic of what you posted.:)

Tried that aswell but still getting the same error messge...
 
Upvote 0
Just tried that but I'm still egtting the same error messge..
Tried which one? You have three replies...
 
Upvote 0
shukhar

You've only posted a small section of your code.

There appears to be some sort of loop going on but you've not posted that part.:)

Richard

I swear that I didn't have that line continuation character in the code I copied from the VBE.:)
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,838
Members
449,343
Latest member
DEWS2031

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