Loop without using Macro if possible

jkam61

New Member
Joined
Mar 11, 2011
Messages
4
Dear friends,

I'm trying to resolve the following problem in my excel worksheet. Any help would be greatly appreciated.

I have the following numbers in column A (range is A1:A11):

<table width="64" border="0" cellpadding="0" cellspacing="0"><tbody><tr style="height: 15pt;" height="20"><td style="height: 15pt; width: 48pt;" width="64" align="right" height="20">10</td> </tr> <tr style="height: 15.75pt;" height="21"> <td style="height: 15.75pt;" align="right" height="21">11</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">12</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">11</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">13</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">13</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">13</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">12</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">12</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">10</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">11</td> </tr> </tbody></table>
I need to enter the following conditions in column B (Range B1:B11):

Starting at current cell:

1. If the value of the cell above is lower than current cell enter 1 in column B.
2. If cell above is greater than current cell, enter -1.
3. This is the tricky part. If cell above equals to current cell, go and check until you find a cell above that is not equal to current cell and then repeat 1 and 2.

The end result looks like this:

<table width="128" border="0" cellpadding="0" cellspacing="0"><col style="width: 48pt;" width="64" span="2"> <tbody><tr style="height: 15pt;" height="20"> <td style="height: 15pt; width: 48pt;" width="64" align="right" height="20">10</td> <td style="width: 48pt;" width="64">
</td> </tr> <tr style="height: 15.75pt;" height="21"> <td style="height: 15.75pt;" align="right" height="21">11</td> <td align="right">1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">12</td> <td align="right">1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">11</td> <td align="right">-1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">13</td> <td align="right">1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">13</td> <td align="right">1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">13</td> <td align="right">1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">12</td> <td align="right">-1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">12</td> <td align="right">-1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">10</td> <td align="right">-1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" align="right" height="20">11</td> <td align="right">1</td> </tr> </tbody></table>
I will need to do it for thousands of rows, so using bunch of "IF" statements won't do it.

Thank you,
Jay
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I just wrote and tried once hope this works.
Try


Code:
[/FONT]
[FONT=Courier New]Sub try()
Dim i As Long
For i = 2 To 11
  If Range("A" & i).Value > Range("A" & i).Offset(-1, 0).Value Then
   Range("B" & i).Value = "-1"
  ElseIf Range("A" & i).Value < Range("A" & i).Offset(-1, 0).Value Then
   Range("B" & i).Value = "1"
  ElseIf Range("A" & i).Value = Range("A" & i).Offset(-1, 0).Value Then
   Range("A" & i).Select
   Do Until ActiveCell.Value <> Range("A" & i).Offset(-1, 0).Value
   ActiveCell.Offset(0, 1).Select
   Loop
   ActiveCell.Value = "Here"
   Else
   End If
Next i[/FONT]
[FONT=Courier New]End Sub
 
Upvote 0
Thank you. I'm new to excel. I understand that this is a macro. Could you explain to me how to implement this, so I can see if it works? Thanks.
 
Upvote 0
Or if you could send the file you tried to my yahoo email: jkam61, it would be greatly appreciated. Thanks.
 
Upvote 0
Couldn't you just use this formula in B2 and copy it down?

Excel Workbook
AB
110
2111
3121
411-1
5131
6131
7131
812-1
912-1
1010-1
11111
Formula
 
Upvote 0
Thank you very much, Peter. I couldn't thank you enough. The simplicity of your solution makes you a true genius in my eyes. I greatly appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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