HOw to fix this problem...Esle without if

nader

Board Regular
Joined
Sep 26, 2005
Messages
135
I have a problem with this code
It show me : Compile erro - Esle without if
I put if then it show me the line in red
how to fix this problem
and Thank you for help


Code:
Private Sub CommandButton1_Click()
Dim j As Integer
j = 8
For i = i - 8 To i - 5
Sheets("sheet2").Cells(j, 5).Value = Sheets("sheet1").Cells(i, 1).Value
Sheets("sheet2").Cells(j + 3, 1).Value = Sheets("sheet1").Cells(i, 3).Value
Sheets("sheet2").Cells(j + 3, 2).Value = Sheets("sheet1").Cells(i, 4).Value
Sheets("sheet2").Cells(j + 3, 3).Value = Sheets("sheet1").Cells(i, 5).Value
Sheets("sheet2").Cells(j + 3, 4).Value = Sheets("sheet1").Cells(i, 6).Value
Sheets("sheet2").Cells(j + 6, 4).Value = Sheets("sheet1").Cells(i, 7).Value
Sheets("sheet2").Cells(j + 6, 3).Value = Sheets("sheet1").Cells(i, 8).Value
Sheets("sheet2").Cells(j + 6, 9).Value = Sheets("sheet1").Cells(i, 9).Value
j = j + 16
Next
If CommandButton1.Enabled = False Then CommandButton1.Enabled = True
CommandButton1.Caption = "Forward  " & i & " - " & i + 3

'i = i - 4

If i - 4 < 8 Then CommandButton2.Enabled = False

Else: CommandButton2.Caption = "backward   " & i - 8 & " - " & i - 5
End If

End sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The problem is in the structure of the IF/THEN/ELSE/ENDIF statement.
Try changing it to:
Code:
If i - 4 < 8 Then
    CommandButton2.Enabled = False
Else
    CommandButton2.Caption = "backward   " & i - 8 & " - " & i - 5
End If
In the earlier statement:
Code:
If CommandButton1.Enabled = False Then CommandButton1.Enabled = True
there is no 'else' so it is OK to have it on one line.
I would always recommend structuring IF/THEN/ELSE/ENDIF (also just IF/THEN/ENDIF) as shown above - it not only avoids this problem, particularly if you later decide to add the 'else', but it makes for easier readability and maintenance.
 
Upvote 0

Forum statistics

Threads
1,217,448
Messages
6,136,685
Members
450,024
Latest member
Beagle263

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