If then macro question

warpedone

Board Regular
Joined
May 1, 2002
Messages
139
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
Joined
May 19, 2002
Messages
2,916
How about something like this:

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
 
Upvote 0

Juan Pablo González

MrExcel MVP
Joined
Feb 8, 2002
Messages
11,959
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
 
Upvote 0

Forum statistics

Threads
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.
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
Top