Compile Error: Else without If (after Adding an Internal If Then Statement)

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
The 2nd else does not attach to the first if, and the Next does not attach to the For Each. How do I correct this? - Thanks, Rowland

Code:
For Each Cell In acct
    If Left(Cell, 2) = "43" Then 'change from Left(Cell, 3) = "400"
        Cell.Font.Bold = True
        IO = Left(Cell, 9) 'change from IO = Right(Left(Cell, 9), 6) '(removed 400 front old acct#)
        LE = Cell.Offset(0, 1) 'SAP Budget number
        Cost = Cell.Offset(0, 4) 'SAP Total=Actuals+Commtmts
    'find internal order on template; set Total range on find, _
        then move to correct Actuals column
        Mktg.Activate 'Activates "Report" sheet
        Set Total = code.Find(IO, LookIn:=xlValues)
            ElseIf Not Total Is Nothing Then
                Set Total = Total.Offset(0, 2) 'Identifies Actuals destination
                Set Budget = Total.Offset(0, 2) 'identifies Budget destination
                Total.Value = Cost 'Populates Actuals (with Total)
                Budget.Value = LE 'Populates Budget
            Else: MsgBox "Missing acct Number " & IO
    Else: End If
    CR.Activate 'Activates "SAP Snapshot" sheet
Next
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Replace Else: End If with End if.
 
Upvote 0
Thanks, no longer compile error but code is wrong, I actually want it to do:
Code:
If Not Total Is Nothing Then
Set Total = Total.Offset(0, 2) 'Identifies Actuals destination
Set Budget = Total.Offset(0, 2) 'identifies Budget destination
Total.Value = Cost 'Populates Actuals (with Total)
Budget.Value = LE 'Populates Budget

But I get a compile error with that, entire code:
Code:
For Each Cell In acct
    If Left(Cell, 2) = "43" Then 'change from Left(Cell, 3) = "400"
        Cell.Font.Bold = True
        IO = Left(Cell, 9) 'change from IO = Right(Left(Cell, 9), 6) '(removed 400 front old acct#)
        LE = Cell.Offset(0, 1) 'SAP Budget number
        Cost = Cell.Offset(0, 4) 'SAP Total=Actuals+Commtmts
    'find internal order on template; set Total range on find, _
        then move to correct Actuals column
        Mktg.Activate 'Activates "Report" sheet
        Set Total = code.Find(IO, LookIn:=xlValues)
            If Not Total Is Nothing Then
                Set Total = Total.Offset(0, 2) 'Identifies Actuals destination
                Set Budget = Total.Offset(0, 2) 'identifies Budget destination
                Total.Value = Cost 'Populates Actuals (with Total)
                Budget.Value = LE 'Populates Budget
            ElseIf LE <> 0 And Cost <> 0 Then
                MsgBox "Missing acct Number " & IO
    End If
    CR.Activate 'Activates "SAP Snapshot" sheet
Next
 
Last edited:
Upvote 0
You are missing an End If you can just add it below/above the existing one.
Code:
    For Each Cell In acct
        If Left(Cell, 2) = "43" Then    'change from Left(Cell, 3) = "400"
            Cell.Font.Bold = True
            IO = Left(Cell, 9)    'change from IO = Right(Left(Cell, 9), 6) '(removed 400 front old acct#)
            LE = Cell.Offset(0, 1)    'SAP Budget number
            Cost = Cell.Offset(0, 4)    'SAP Total=Actuals+Commtmts
            'find internal order on template; set Total range on find, _
             then move to correct Actuals column
            Mktg.Activate    'Activates "Report" sheet
            Set Total = code.Find(IO, LookIn:=xlValues)
            If Not Total Is Nothing Then
                Set Total = Total.Offset(0, 2)    'Identifies Actuals destination
                Set Budget = Total.Offset(0, 2)    'identifies Budget destination
                Total.Value = Cost    'Populates Actuals (with Total)
                Budget.Value = LE    'Populates Budget
            ElseIf LE <> 0 And Cost <> 0 Then
                MsgBox "Missing acct Number " & IO
            End If

        End If
        CR.Activate    'Activates "SAP Snapshot" sheet
    Next
 
Upvote 0

Forum statistics

Threads
1,216,174
Messages
6,129,296
Members
449,498
Latest member
Lee_ray

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