View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ryan H Ryan H is offline
external usenet poster
 
Posts: 489
Default looping thru rows

Here is an example of using the For Each...Next Loop instead of the Do...Loop
that I gave you in the earlier post. Hope this helps! If so, let me know,
click "YES" below.

Sub Z_UpdateCell()

Dim LastRow As Long
Dim MyRange As Range
Dim rng As Range


With Sheets("ListofDiff")

' find last row in Col. J
LastRow = .Cells(Rows.Count, "J").End(xlUp).Row

' set range to loop thru
Set MyRange = .Range("J2:J" & LastRow)

For Each rng In MyRange

' test if rng is empty, if so skip it
If Not IsEmpty(.Cells(rng.Row, "J")) Then
If .Cells(rng.Row, "J") < 0 Then
.Cells(rng.Row, "N") = -(.Cells(rng.Row, "L"))
Else
.Cells(rng.Row, "N") = .Cells(rng.Row, "L")
End If
End If
Next rng
End With

End Sub

--
Cheers,
Ryan


"Ray" wrote:

POSTED: SEARCH - LOOPING, IN EXCEL PROGRAMMING

Hello,

I am trying convert a value of one column (col 12 is value) to negative if
the value in another column (col 10 is cs qty) is negative. The result would
update to column 14 as - or + value.

The function below works but I cant get it to loop thru the entire rows. The
amount of records changes daily. I am trying to loop this using do until but
I cant get it to work. Would you please show me how to get this to loop thru
the entire rows?

Function Z_UpdateCell()

If Worksheets("ListofDiff").Cells(2, 10) < 0 Then
Worksheets("ListofDiff").Cells(2, 14) = -(Cells(2, 12))
Else: Worksheets("ListofDiff").Cells(2, 14) = (Cells(2, 12))
End If


End Function

thanks, ray