If then else compile error: Else without If

talt0612

New Member
Joined
Nov 22, 2011
Messages
35
Hi all-
I'm trying to run a simple loop and I keep getting "compile error: Else without if" that doesn't make sense to me.

Basically I'm checking a range for formulas and if there is a formula, I'll skip that cell. If the cell is blank, I'll enter a formula in an adjacent cell. Any help from more experienced users is appreciated. Here's what I'm working with:

Code[
Sub hedgeme()
Dim MyCell As Range, MyRange As Range
Set MyRange = Range("S13:S20")
For Each MyCell In MyRange
If MyCell.HasFormula Then MyCell.Offset(1, 0).Select
Else MyCell.Offset(0, 1).Value = "=IF(RC[-1]<-R2C20,RC[-9]-RC[-7],IF(RC[- -1]>R2C20,+RC[-9]-RC[-7],0))"
End If
Next MyCell
End Sub
]Code
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
If MyCell.HasFormula Then
   MyCell.Offset(1, 0).Select
  Else
   MyCell.Offset(0, 1).Value = "=IF(RC[-1]<-R2C20,RC[-9]-RC[-7],IF(RC[-     -1]>R2C20,+RC[-9]-RC[-7],0))"
         End If

or

Code:
If MyCell.HasFormula Then MyCell.Offset(1, 0).Select Else MyCell.Offset(0, 1).Value = "=IF(RC[-1]<-R2C20,RC[-9]-RC[-7],IF(RC[-     -1]>R2C20,+RC[-9]-RC[-7],0))"

but that's hardly readable.
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,102
Members
449,205
Latest member
ralemanygarcia

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