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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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