I am trying to write a VBA routine that substitutes certain values into cell "B16" (going off a list on a separate workbook), recalculates the entire workbook after each substitution and records the value of the cell "I31" (post recalculation) in another workbook.
The behaviour that I observe is a lot different from what I expect. Suppose that the value of "I31" is 0 before I run the routine and that it needs to be equal to 1 after the first iteration and equal to 2 after the second iteration. When I run the routine, both the recorded values are 0 (instead of 1 and 2) and the value of "I31" itself is 2. The latter makes sense. It seems like Excel is not refreshing the cell values before copying them into other cells. Maybe it is not waiting for 'SendKeys "{F9}"' to terminate before copying the cell values. So I forced the routine to pause for one minute after each iteration (the recalculation should take about ten seconds) and this time both the recorded values are 0 again and "I31" itself is also 0. This is even worse.
On a related note Application.Calculate (or its variants like Worksheet.Calculate) has no effect whatsoever on the worksheet. Nothing is being recalculated when I use those instead of SendKeys "{F9}". This occurs even if the only line in the routine is Application.Calculate. I have no clue why this is happening.
My routine is below. Any help appreciated. Thanks.
Sub createData()
Dim x As Integer
For x = 1 To 2
Range("B16").value = Worksheets("Types").Cells(x, 1)
'Application.Calculate
SendKeys "{F9}"
'Application.Wait Now() + TimeValue("00:01:00")
Worksheets("Results").Cells(x, 1).value = Range("I31").value
Next
End Sub
Application.CalculateFullmay do what you wantxlCalculationAutomatic, until some code (or the user) toggles it otherwise (e.g.xlCalculationManual) - AFAIK calculation being manual is the only way Excel won't automatically recalculate when cells change, barring non-volatile user-defined functions. Research the terms if you're unsure what they are, but if you don't have VBA functions invoked by cells, you don't have UDF's to worry about.Range("...")is implicitly referring to whatever worksheet is currently active, whileWorksheets("Results").Cells(...)is explicitly qualifying the range with a specific worksheet object: assuming that's written in a standard module and not a worksheet's code-behind, your code will behave differently depending on what worksheet is active when it's invoked.