Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
RaY RaY is offline
external usenet poster
 
Posts: 164
Default looping thru rows

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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default looping thru rows

I change your code a bit.

1.) Since you are not returning any kind of data it is not neccessary to
use a Function. So I changed it to a Sub. No big deal though.

2.) I changed your cell references from Cells(2,10) to Cells(2, "J"). I
don't like to label columns by number because it can be hard to debug.

3.) When looping thru a range of cells (or objects) it is recommended to
use the For Each...Next Loop instead of Do...Loop or For...Next Loop. But
since you wanted the Do Until that is what I gave you.

4.) Give this code a try. Hope it helps! If so, let me know, click "YES"
below.

Sub Z_UpdateCell()

Dim LastRow As Long
Dim i As Long

With Sheets("ListofDiff")
LastRow = .Cells(Rows.Count, "J").End(xlUp).Row

i = 2
Do Until i LastRow
If .Cells(i, "J") < 0 Then
.Cells(i, "N") = -(.Cells(i, "L"))
Else
.Cells(i, "N") = .Cells(i, "L")
End If
i = i + 1
Loop
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

  #4   Report Post  
Posted to microsoft.public.excel.programming
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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default looping thru rows

First things first: A function can only return a value to a single cell, so
you need to use a Sub procedure.

Sub Z_UpdateCell()
Dim lr As Long, sh As Worksheet
Set sh = Worksheets("ListofDiff")
lr = sh.Cells(Rows.Count, 10).End(xlUp).Row
For i = 2 To lr
If Worksheets("ListofDiff").Cells(i, 10) < 0 Then
Worksheets("ListofDiff").Cells(i, 14) = -(Cells(i, 12))
Else: Worksheets("ListofDiff").Cells(i, 14) = (Cells(i, 12))
End If
Next

End Sub

See if this works for you. If you get errors post back.



"Ray" wrote in message
...
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





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default looping thru rows

It runs, but as a sub. It cannot be called as a function from a worksheet
cell to return a value. It returns a #Value! error on the worksheet.


"Don Guillett" wrote in message
...
Test mine

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"JLGWhiz" wrote in message
...
First things first: A function can only return a value to a single cell,
so you need to use a Sub procedure.

Sub Z_UpdateCell()
Dim lr As Long, sh As Worksheet
Set sh = Worksheets("ListofDiff")
lr = sh.Cells(Rows.Count, 10).End(xlUp).Row
For i = 2 To lr
If Worksheets("ListofDiff").Cells(i, 10) < 0 Then
Worksheets("ListofDiff").Cells(i, 14) = -(Cells(i, 12))
Else: Worksheets("ListofDiff").Cells(i, 14) = (Cells(i, 12))
End If
Next

End Sub

See if this works for you. If you get errors post back.



"Ray" wrote in message
...
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






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,522
Default looping thru rows

Since the original question was to run for more than one, I didn't change
it. I also would have normally used SUB but didn't in this case. And, it did
as OP desired.

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"JLGWhiz" wrote in message
...
It runs, but as a sub. It cannot be called as a function from a
worksheet cell to return a value. It returns a #Value! error on the
worksheet.


"Don Guillett" wrote in message
...
Test mine

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"JLGWhiz" wrote in message
...
First things first: A function can only return a value to a single
cell, so you need to use a Sub procedure.

Sub Z_UpdateCell()
Dim lr As Long, sh As Worksheet
Set sh = Worksheets("ListofDiff")
lr = sh.Cells(Rows.Count, 10).End(xlUp).Row
For i = 2 To lr
If Worksheets("ListofDiff").Cells(i, 10) < 0 Then
Worksheets("ListofDiff").Cells(i, 14) = -(Cells(i, 12))
Else: Worksheets("ListofDiff").Cells(i, 14) = (Cells(i, 12))
End If
Next

End Sub

See if this works for you. If you get errors post back.



"Ray" wrote in message
...
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






  #9   Report Post  
Posted to microsoft.public.excel.programming
RaY RaY is offline
external usenet poster
 
Posts: 164
Default looping thru rows

Hi Don,
This works if the row count is known. in this case 7; I got my problem
solved with help from Ryan's code. Thanks very much for your help.
Ray

"Don Guillett" wrote:

Try this idea. Notice the dots .......

Function Z_UpdateCell()
With Sheets("ListofDiff")
For i = 2 To 7
If .Cells(i, 10) < 0 Then
.Cells(i, 14) = -.Cells(i, 12)
Else
.Cells(i, 14) = .Cells(i, 12)
End If
Next i
End With
End Function


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Ray" wrote in message
...
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


.

  #10   Report Post  
Posted to microsoft.public.excel.programming
RaY RaY is offline
external usenet poster
 
Posts: 164
Default looping thru rows

Hi Ryan,
Thanks for your help. Sorry for taking so long to check this but I could not
find my post until now. Both of your codes worked well. many thanks, Ray

"Ryan H" wrote:

I change your code a bit.

1.) Since you are not returning any kind of data it is not neccessary to
use a Function. So I changed it to a Sub. No big deal though.

2.) I changed your cell references from Cells(2,10) to Cells(2, "J"). I
don't like to label columns by number because it can be hard to debug.

3.) When looping thru a range of cells (or objects) it is recommended to
use the For Each...Next Loop instead of Do...Loop or For...Next Loop. But
since you wanted the Do Until that is what I gave you.

4.) Give this code a try. Hope it helps! If so, let me know, click "YES"
below.

Sub Z_UpdateCell()

Dim LastRow As Long
Dim i As Long

With Sheets("ListofDiff")
LastRow = .Cells(Rows.Count, "J").End(xlUp).Row

i = 2
Do Until i LastRow
If .Cells(i, "J") < 0 Then
.Cells(i, "N") = -(.Cells(i, "L"))
Else
.Cells(i, "N") = .Cells(i, "L")
End If
i = i + 1
Loop
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



  #12   Report Post  
Posted to microsoft.public.excel.programming
RaY RaY is offline
external usenet poster
 
Posts: 164
Default looping thru rows

Hello,

This works perfect. Sorry for taking so long. I could not find my post
question. Also, thanks for pointing out "first things first" in regards to
using functions instead of sub. I will remember that. Thanks again, Ray

"JLGWhiz" wrote:

First things first: A function can only return a value to a single cell, so
you need to use a Sub procedure.

Sub Z_UpdateCell()
Dim lr As Long, sh As Worksheet
Set sh = Worksheets("ListofDiff")
lr = sh.Cells(Rows.Count, 10).End(xlUp).Row
For i = 2 To lr
If Worksheets("ListofDiff").Cells(i, 10) < 0 Then
Worksheets("ListofDiff").Cells(i, 14) = -(Cells(i, 12))
Else: Worksheets("ListofDiff").Cells(i, 14) = (Cells(i, 12))
End If
Next

End Sub

See if this works for you. If you get errors post back.



"Ray" wrote in message
...
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



.

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
looping to through rows [email protected] Excel Programming 1 August 17th 07 01:59 AM
looping through columns and rows Guerilla Excel Programming 4 March 22nd 07 09:31 PM
looping through rows and columns mattguerilla Excel Discussion (Misc queries) 1 March 20th 07 05:14 PM
Not looping through rows bg18461[_18_] Excel Programming 2 August 4th 06 04:35 PM
Looping through Columns then Rows JCP Excel Programming 3 October 30th 05 04:27 AM


All times are GMT +1. The time now is 05:54 PM.

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

About Us

"It's about Microsoft Excel"