Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Efficient If's loop

Hi all!

I have to test a column ("A") of numeric data and to categorize it (column
B) according to its value and or to the value of the cell below. For example,
if the value of A2 = 0 B2 value should be set to 1 , if the value of A2 < 0
B2 value should be set to 2. If the the value of A2 0 should be set to 4 if
A3 - A2 = 0 or A2 - A1 = 0 otherwise it should be set to 3.

I thought about the following code, but since the list at column A can reach
easialy 10000 cells, I am not sure that it is efficient.

x = 2

Do while Range("C" & lastrow) < 0
y = x + 1
z = x - 1

If Range("A" & x).Value = 0 Then
Range("B" & x).Value = 1
End If
If Range("A" & x).Value < 0 Then
Range("B" & x).Value = 2
End If
If Range("A" & y).Value - Range("A" & x).Value = 0 or Range("A" & x).Value
-Range("A" & z).Value = 0 Then
Range("B" & x).Value = 4
Else
Range("B" & x).Value = 3
End If

x = x + 1

Loop

Any suggestion for more efficient code?

Thanks in advance

Eli
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Efficient If's loop

Maybe this

Sub stantial()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set MyRange = Range("A1:A" & LastRow)
For Each c In MyRange
If c.Offset(1, 0).Value - c.Value = 0 Then
c.Offset(, 1).Value = 3
ElseIf c.Value < 0 Then
c.Offset(, 1).Value = 0
ElseIf c.Value 0 Then
c.Offset(, 1).Value = 4
ElseIf c.Value = 0 Then
c.Offset(, 1).Value = 1
End If
Next

End Sub

Mike

"אלי" wrote:

Hi all!

I have to test a column ("A") of numeric data and to categorize it (column
B) according to its value and or to the value of the cell below. For example,
if the value of A2 = 0 B2 value should be set to 1 , if the value of A2 < 0
B2 value should be set to 2. If the the value of A2 0 should be set to 4 if
A3 - A2 = 0 or A2 - A1 = 0 otherwise it should be set to 3.

I thought about the following code, but since the list at column A can reach
easialy 10000 cells, I am not sure that it is efficient.

x = 2

Do while Range("C" & lastrow) < 0
y = x + 1
z = x - 1

If Range("A" & x).Value = 0 Then
Range("B" & x).Value = 1
End If
If Range("A" & x).Value < 0 Then
Range("B" & x).Value = 2
End If
If Range("A" & y).Value - Range("A" & x).Value = 0 or Range("A" & x).Value
-Range("A" & z).Value = 0 Then
Range("B" & x).Value = 4
Else
Range("B" & x).Value = 3
End If

x = x + 1

Loop

Any suggestion for more efficient code?

Thanks in advance

Eli

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Most efficient way to loop through a folder and find all the XLS f Barb Reinhardt Excel Programming 3 October 14th 08 07:35 PM
Too many 'if's! Ben Mehling Excel Discussion (Misc queries) 4 May 11th 07 01:31 PM
Too Many If's Rianne Excel Worksheet Functions 10 March 22nd 07 08:13 PM
to many 'IF's'?? boufant Excel Worksheet Functions 4 July 2nd 05 10:58 AM
Loop more efficient ? farmer[_2_] Excel Programming 4 June 21st 04 06:51 AM


All times are GMT +1. The time now is 10:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright 2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"