View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
JLatham JLatham is offline
external usenet poster
 
Posts: 2,203
Default Saving Excel Pro 2003 iteration results

Tunja,
Since you say it's writing the results to the same sheet, I would say that
you managed to put the code into the proper place. The code below can be
used to replace the other code, and you just need to make 2 changes to it
this time so that the results will be written to the other sheet. Change the
"E2" as before, and in the statement not far below it that reads:
Const resultSheetName = "myResultsSheet"
change the myResultsSheet to the name of the sheet that you wish the results
to be placed onto.

As for the IF statement, we could probably work that out to include in the
code, but I would need to know the rules of when a result is valid to copy as
it was calculated to be and when to write the "there is no logical solution"
entry to the results sheet.

Private Sub Worksheet_Change(ByVal Target As Range)
'address of the first cell to save results into
'all cells below it must not be used for anything
'other than saving results
Const firstResultsCell = "E2" ' change as required
'change this to the name of the sheet you wish the
'results to be recorded on
Const resultSheetName = "myResultsSheet"
Dim resultsSheet As Worksheet

'cell address where you enter the angle
Const angleEntryCell = "A1"
'address of the cell with the calculated area
Const areaResultCell = "A2"
Dim nextResultsRow As Long

If Application.Intersect(Target, _
Range(angleEntryCell)) Is Nothing Then
'did not change the angle
Exit Sub
End If
If Target = 0 Then
'cleared the entry, do not save new result
Exit Sub
End If
'save the results in the first empty
'cell below 'firstResultsCell'
'or in 'firstResultsCell if it is empty
Set resultsSheet = ThisWorkbook.Worksheets(resultSheetName)
nextResultsRow = resultsSheet.Cells(Rows.Count, _
Range(firstResultsCell).Column).End(xlUp).Row + 1
If nextResultsRow < Range(firstResultsCell).Row Or _
IsEmpty(resultsSheet.Range(firstResultsCell)) Then
nextResultsRow = Range(firstResultsCell).Row
End If
resultsSheet.Range(firstResultsCell).Offset(nextRe sultsRow - _
Range(firstResultsCell).Row, 0) = _
Range(areaResultCell)
End Sub

"Tunja" wrote:

I followed your instruction. Thank you very much. The notes were perfect too.

- How may I save the results in another sheet? (Right now I can get the
results in the same sheet)

- May I put "IF statement" in Visial Basic part?
Because my iteration formulation is not very good. My Area results suppose
to obey 2 parameters. I could not put those 2 conditions in IF so, right now
iteration finds a value by using the first parameter, if it doesn't cover
second statement, then I manually change the formula in iteration and have it
solved the area according the second parameter. Even if the area result
doesn't cover the statements after using the second parameter then I say "for
this angle, there is no logical solution to myself" and I increase the Angle.

Since my "IF statement" in iteration is not very well prepared (my bad),
sometimes I may have to change couple of things in the formula. Each time I
change the formula, excel calculates the iteration of course. For every new
result, visial basic writes a new result as we(you) asked from it.

Since I can not make my IF statement in iteration better, maybe I could make
the visial basic part better by adding IF. So I was wondering if I may use IF
or WHEN statement in Visial Basic.

OR I may study IF AND statement in iteration part and try to fit my 2
conditions in my iteration(I am not sure if it will work) :)

It would be awesome if you may let me know how to have visial basic write
the results in another sheet.

Thank you very very much.
I appreciate your help.

Regards
"JLatham" wrote: