# If then macro question

#### warpedone

##### Board Regular
For i = 1 to x
If i = 1 do something
If i = x do something else
if i is between 1 and x do something else entirely
Next i

My problem is in the If i is between 1 and x statement. All my i's are entering this if every time and it's recalculating w(1) and w(x).

The code I currently have that doesn't work is:
If 1 < i < x Then

### Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

#### Seti

##### Well-known Member

Code:
`````` For i= 1 to x
If i = 1 then
do something
Elseif i = x then
do another something
Else
do a third thing  ' this will handle alll case of 1 < i < x
End if
Next i``````

#### warpedone

##### Board Regular
Never mind....I figured it out.

#### Juan Pablo González

##### MrExcel MVP
You can do it like

Code:
``If i > 1 And i <x Then``

but I would use either an ElseIf

Code:
``````If i = 1 Then

ElseIf i < x Then

Else

End If``````

or a Select Case statement

Code:
``````Select Case i
Case 1
'Code
Case Is < x
'Code
Case Else
'Code
End Select``````

Replies
8
Views
202
Replies
1
Views
83
Replies
1
Views
115
Replies
1
Views
127
Replies
2
Views
272

1,195,590
Messages
6,010,611
Members
441,558
Latest member
lambierules

### 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.

### Which adblocker are you using?

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

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