I want to write an Excel macro: I have a few calculations with a starting value. There are over 100 of different starting numbers and I want to automate copying the starting value in the right cell, some other cells have formulas which read this value and work with it. These formulars are fetching some data from the web (afaik), calculations in some other sheets and it takes some seconds to calculate. The cells in the sheet I'm working are just links to other cells in other sheets, so the formular isn't directly in the "working-sheet" (sheet #1). I don't know what these formulas exactly do, they are not from me. For each of the starting numbers I want to copy the results into a another sheet.
Private Sub CommandButton1_Click()
Application.Calculation = xlCalculationManual
For i = 2 To Sheets(2).Range("A1048576").End(xlUp).Row
Sheets(1).Cells(5, 3).Value = Sheets(2).Cells(i, 3).Value
Application.CalculateFull
Sheets(2).Cells(i, 4).Value = Sheets(1).Cells(7, 3).Value
Sheets(2).Cells(i, 5).Value = Sheets(1).Cells(8, 3).Value
Sheets(2).Cells(i, 6).Value = Sheets(1).Cells(9, 3).Value
Sheets(2).Cells(i, 7).Value = Sheets(1).Cells(10, 3).Value
Sheets(2).Cells(i, 8).Value = Sheets(1).Cells(11, 3).Value
Sheets(2).Cells(i, 9).Value = Sheets(1).Cells(12, 3).Value
Sheets(2).Cells(i, 10).Value = Sheets(1).Cells(15, 3).Value
Sheets(2).Cells(i, 11).Value = Sheets(1).Cells(16, 3).Value
Next i
Application.Calculation = xlCalculationAutomatic
i = 0
End Sub
So I copy from the Sheet(2) the starting-number (C-column) into the Cell(C5) of Sheet(1). When the Value of the Cell(C5) in Sheet(1) is changed, the values in Sheet(1).Range(C7:C16) are getting recalculated. I copy the new calculated values back into Sheet(2), next into the row of the proper starting-number. So Sheet(1).Range(C7:C16) (except of two values) in the Sheet(2).Range(Di:Ki) (i, index for the starting-number). And this looped for every starting-number.
So the Range(C7:C16) holds the results. My problem is, that the macro doesn't wait for the calculation to be finished. All cells in the range(Di:Ki) are getting the same values (the values which are in the range(C7:C16) before I start the macro).
I tried it in different ways, as seen with Application.CalculateFull, with
Do Until Application.CalculationState = xlDone
DoEvents
Loop
and also with a Worksheet_Calculate() sub. I also tried Worksheet_Change() but it doesn't fetch the formula calculations.
When I use simple formulas like basic addition etc. it does work: E.g. Formula for Sheet(1).C7 is C5+1 (starting-number+1), I'll get in Sheet(2)
Starting Number - Result from Sheet(1).C7
1 - 2
2 - 3
3 - 4
Maybe someone can help me out? Thanks in advance!