Okay, here we go. Try this one on for size. I'm not sure that I really got
the tests and conditions right, but I tried to follow your instructions as
precisely as I could.
This code contains a Do...Loop section of code in it. From your
instruction, I understand this code in your worksheet will eventually produce
a sum (called myvalue in the code) that is equal to zero. If, however,
myvalue never reaches 0, the code will continue to run through that loop
almost indefinitely (until the computer runs out of memory for the number it
is supposed to store in myvalue)....
All that is to say, before you start troubleshooting the code, put a
breakpoint within the Do...Loop section. That way, if the code isn't working
right, you have a way out. When you do that, the code will stop at the
breakpoint every time, and to continue you will have to push the "play"
button in
VB. You can set a breakpoint by putting the cursor on the line
you want and press F9, or by choosing Toggle breakpoint in the Debug menu, or
by clicking in the left margin of the code. You should see a red highlight
across the line of code and a red dot to the left.
Also, while troubleshooting, I would put the code in a module. Then, when
you are satisfied it is working right, then move it to the Worksheet_Change()
module.
Here it is:
Sub myloop()
Dim j As Integer
For j = 82 To 164 'start looking through cells A82 to A164
myvalue = 0
If Cells(j, 1) <= 10000 Then
'do nothing
Else
For k = j + 1 To 165
If Cells(k, 1).Value 10000 Then 'start looking and testing
cells in the j+1 'row to row 165 in column A
For l = 82 To k ' loop to sum cells in column C from row 82
to row k
myvalue = myvalue + Cells(l, 3).Value
Next l
If myvalue = 0 Then
Exit For ' go to the outer loop, check cell j+1
Else 'check conditions if sum wasn't zero.
myincr = Cells(j, 2).Value
Do
myvalue = Empty
myincr = myincr + 10
Cells(j, 2).Value = myincr
If Cells(j, 3) 999999 Then
Cells(j, 2).Value = Cells(j, 2).Value - 10
Exit Do
End If
For l = 82 To k
myvalue = myvalue + Cells(l, 3).Value
Next l
If myvalue = 0 Then
Exit Do
End If
Loop
End If
Exit For 'once all the above conditions are met, move on to
the j+1 row
End If
Next k
End If
Next j
End Sub
Good Luck,
drhalter
"Dean" wrote:
I have not made any progress on a macro. I can troubleshoot and edit some
macros (and, of course, record some), but just don't do it frequently enough
to have gotten good enough to attempt to write it. I'm an EXCEL jockey from
the olden days, but never really learned VBE.
If you'd like to take a shot at it, from what I've posted, or ask questions
first, that would be wonderful. I might be able to take it to the finish
line from there. Mostly, it's the syntax where I get thrown.
Thanks!
Dean
"drhalter" wrote in message
...
Sounds like a nice little problem. I thought I'd work on it a bit, but it
might be easier if you post the code you've already come up with.
drhalter
"Dean" wrote:
I was wondering if anyone could help me with creating a macro procedure
to
do the following, which is a pain to do manually, and needs to be redone
every time anyone changes anything in my spreadsheet, which if often! I
think I've learned enough so that I can troubleshoot the code, should
there
be any problem, if someone can supply it to me. By the way, the columns
are
really, AU, BH, and AV but I thought it would be easier to ask for A, C,
and
B below, and edit that myself. Please let me know if I have left
anything
out.
From rows 82 to 164, inclusive, I want to check Column A, one row at a
time
(each row is one month later). If the entry is <= 10,000, then there is
nothing needed, so I want the cursor to simply move down to the next
entry
in that column. If, in the Jth row, the entry is 10,000, then it needs
to
do a test. It needs to look forward to the next row in this same column
A
where there is another entry greater than 10,000 - let's call it the Kth
row
(K <= 165, at some point, there will be no such 'future' value greater
than
10,000, so then set K = 165). If the sum of entries in another column,
column C, from row 82 until (and including) this 'future' row K is zero,
then, once again, no action is required and we should just move down one
row
(to J+1) in column A.
However, if the above test fails to produce zero, then I want to slide
the
cursor sideways over to the Jth row of column B and enter the value 10,
then
repeatedly increment by 10 more, until the second condition above is met.
Occasionally, before this condition is met, another increment of 10
causes
the entry in row J of column C to exceed 999,999. If so, then I want to
back off to the prior increment of 10 and end the procedure for the Jth
row.
Then, go back to column A and continue with the J+1st row, ending at row
164.
Thanks so much!
Dean