Must run macro twice for it to work (but fine on step-through)

mitikthewalrus

New Member
Joined
Nov 11, 2015
Messages
1
I have a short VBA macro that calls a sequence of 6 near-identical subroutines. I've provided one of them below (Year0) as a sample; the others just reference different rows on the same worksheet. If I step through AllYears it works fine to completion, but when I run it, it seems to skip the subs Year1-Year5 and says the calculation is complete without performing the actions. No error messages are given (i.e., the macro doesn't exit early), and the "t" is reported as 0. If I immediately run the macro again, it succeeds with all calculations in 0-1 sec. It consistently runs on the second click, but never the first.

I have already confirmed that "Enable Background Refresh" is unchecked. I tried inserting time delays after each sub, but that didn't seem to help, either. Calculation is automatic at the outset. Any ideas?

Excel 2013
Windows 8
64-bit


Code:
Sub AllYears()
'
' Runs base year and all five projection years in sequence
'
    Application.Calculation = xlCalculationManual
       
    Dim t As Date
    t = Now()
       
    Application.ScreenUpdating = False
    
    Sheets("USCN").Select
    Year0
    Year1
    Year2
    Year3
    Year4
    Year5

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Sheets("Output - Live").Select
    MsgBox "Calculation Complete  ( " & Format(Now() - t, "s") & "s )" 

End Sub



Sub Year0()
    
    Dim limit As Integer
    limit = 1000
    Dim count As Integer
    count = 1
    
    Range("DT120").Value = Range("AF97").Value
    Range("A105:DT124").Calculate
    
    Do While (Range("DI122") > 0.00001 And count < limit)
        Range("DI107:DR118").Value = Range("D107:M118").Value
        Range("DT120").Value = Range("AC122").Value
        Range("A105:DT124").Calculate
        count = count + 1
    Loop
    
    If count >= limit Then
        MsgBox ("No solution found for " & ActiveSheet.Name & " -- exiting macro.")
        End
    End If
   
End Sub
 
Last edited:

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top