# Oscillating Procedure

I wrote a procedure which is designed to display three values: a total 12 mo average, the 12 mo average for groups with the percentage of the total of those groups; and the 12 mo average for individuals with the percentage of the total for those individuals.
The following procedure returns the correct value for the total 12 mo average every time. One time it is run, the 12 mo average for groups is correct, but the 12 mo average for individuals is incorrect. The next time the procedure is run, the 12 mo average for the groups is incorrect, but the 12 mo average for individuals is correct. It oscillates back and forth. What is causing that? I can not figure it out.

local lv12mosum, lvgroups, lvindividuals
opensheet
selectall
field Â«12mosumÂ»
total
lastrecord
copycell
lv12mosum=val(clipboard())/12
removesummaries 7
selectall
field Â«typeÂ»
select Â«typeÂ»=â€śgroupâ€ť
field Â«12mosumÂ»
total
lastrecord
copycell
lvgroups=val(clipboard())/12
removesummaries 7
selectall
field Â«typeÂ»
select Â«typeÂ»=â€śindividualâ€ť
field Â«12mosumÂ»
total
lastrecord
copycell
lvindividuals=val(clipboard())/12
removesummaries 7
selectall
message â€śThe 12moave is: â€ś+pattern(lv12mosum,â€ť\$#,.##â€ť)+cr()+cr()+â€śFrom groups: â€ś+pattern(lvgroups,â€ť\$#,.##â€ť)+" ("+pattern((lvgroups100)/lv12mosum,"##.#")+"%)"+cr()+cr()+â€śFrom individuals is: â€ś+pattern(lvindividuals,â€ť\$#,.##â€ť)+" ("+pattern((lvindividuals100)/lv12mosum,"##.#")+"%)"
closewindow
beep

My guess is that the clipboard is the cause of your problem so I would replace the three instances of code:

``````copycell
lv12mosum=val(clipboard())/12
``````

with:

``````lv12mosum=Â«12mosumÂ»/12
``````

andâ€¦

``````copycell
lvgroups=val(clipboard())/12
``````

with:

``````lvgroups=Â«12mosumÂ»/12
``````

and finallyâ€¦

``````copycell
lvindividuals=val(clipboard())/12
``````

with:

``````lvindividuals=Â«12mosumÂ»/12
``````

You are already putting the focus on the cell you want so there is no need to copy it to the clipboard for processing - just use it directly in the code.

I think you can do this much more efficiently by using aggregate statements. You do not need to open the datasheet or make any selections or calculate any totals. Like this:

``````selectall
let lv12mosum=aggregate({Â«12mosumÂ»},"sum")/12
let lvgroups=aggregate({Â«12mosumÂ»},"sum",{type="group"})/12
let lvindividuals=aggregate({Â«12mosumÂ»},"sum",{type="individual"})/12
message ....``````
1 Like

This was really helpful. Iâ€™m still learning how to use some of the newer functions in PanX. This was much faster and did the job every time. Thanks again.