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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,223,275
Messages
6,171,127
Members
452,381
Latest member
Nova88

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