If Else

30percent

Board Regular
Joined
May 5, 2011
Messages
123
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have the following code. I think I have the syntax correct but somehow I got error message: Compile error: Else without If. Not sure why. Wonder if someone could help point out the problem?

Code:
finalRow = sht.Cells(sht.Rows.Count, "a").End(xlUp).Row
finalRow_minMax = sht_minMax.Cells(sht.Rows.Count, "a").End(xlUp).Row


For i = 2 To finalRow
    
    If Cells(i, 14).Value >= 0 Then
        Bucket = Cells(i, 14).Value
        Select Case Bucket
        Case 50
            Cells(i, 15).Value = Cells(i, 5).Value + 0.001
        Case 40
            Cells(i, 15).Value = Cells(i, 5).Value + 0.002
        Case 30
            Cells(i, 15).Value = Cells(i, 5).Value + 0.003
        Case 20
            Cells(i, 15).Value = Cells(i, 5).Value + 0.004
        Case 10
            Cells(i, 15).Value = Cells(i, 5).Value + 0.005
        Case 0
            Cells(i, 15).Value = Cells(i, 5).Value + 0.006
    Else
        Select Case Bucket
        Case -10
            Cells(i, 15).Value = Cells(i, 5).Value - 0.001
        Case -20
            Cells(i, 15).Value = Cells(i, 5).Value - 0.002
        Case -30
            Cells(i, 15).Value = Cells(i, 5).Value - 0.003
        Case -500
            Cells(i, 15).Value = Cells(i, 5).Value - 0.004
       End Select
    End If
    
Next
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try
Code:
finalRow = sht.Cells(sht.Rows.Count, "a").End(xlUp).Row
finalRow_minMax = sht_minMax.Cells(sht.Rows.Count, "a").End(xlUp).Row


For i = 2 To finalRow
    
    If Cells(i, 14).Value >= 0 Then
        Bucket = Cells(i, 14).Value
        Select Case Bucket
        Case 50
            Cells(i, 15).Value = Cells(i, 5).Value + 0.001
        Case 40
            Cells(i, 15).Value = Cells(i, 5).Value + 0.002
        Case 30
            Cells(i, 15).Value = Cells(i, 5).Value + 0.003
        Case 20
            Cells(i, 15).Value = Cells(i, 5).Value + 0.004
        Case 10
            Cells(i, 15).Value = Cells(i, 5).Value + 0.005
        Case 0
            Cells(i, 15).Value = Cells(i, 5).Value + 0.006
       [COLOR=#ff0000]End Select[/COLOR]
    Else
        Select Case Bucket
        Case -10
            Cells(i, 15).Value = Cells(i, 5).Value - 0.001
        Case -20
            Cells(i, 15).Value = Cells(i, 5).Value - 0.002
        Case -30
            Cells(i, 15).Value = Cells(i, 5).Value - 0.003
        Case -500
            Cells(i, 15).Value = Cells(i, 5).Value - 0.004
       End Select
    End If
    
Next
 
Upvote 0
Do you need the If?
Code:
        Select Case Bucket
            Case 50
                Cells(i, 15).Value = Cells(i, 5).Value + 0.001
            Case 40
                Cells(i, 15).Value = Cells(i, 5).Value + 0.002
            Case 30
                Cells(i, 15).Value = Cells(i, 5).Value + 0.003
            Case 20
                Cells(i, 15).Value = Cells(i, 5).Value + 0.004
            Case 10
                Cells(i, 15).Value = Cells(i, 5).Value + 0.005
            Case 0
                Cells(i, 15).Value = Cells(i, 5).Value + 0.006
            Case -10
                Cells(i, 15).Value = Cells(i, 5).Value - 0.001
            Case -20
                Cells(i, 15).Value = Cells(i, 5).Value - 0.002
            Case -30
                Cells(i, 15).Value = Cells(i, 5).Value - 0.003
            Case -500
                Cells(i, 15).Value = Cells(i, 5).Value - 0.004
        End Select
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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