By chance I created a vba race condition?

I was hoping to get some idea of ​​the error that I could not reproduce.

I have a very complicated worksheet that changes many variables to get a specific cell aa5to return a row. if any condition is not met, it returns 0. formula for this cell

=IF(SUM(AA2:AA4)=0,SubItem,0)

where aa2:aa4are conditions that must be 0 in order to return a row, and subitemis a named range of cells.

As soon as it returns the string, I have a module for inserting a cell aa5on another sheet export. The problem is that after running the subroutine (it takes about 20 minutes) I find 0 values ​​on the sheet export.

I tried manually changing all the variables to a condition that created the error and nothing appears. I also tried to run the code line by line and cannot reproduce it there.

My last straw was inserted into a module inserted into a sheet export

If Worksheets("analysis").Range("aa5").Value = 0 Then
Exit Sub

And yet I have 0 values ​​after starting!

I'm really not a programmer, but I have some experience with VBA code, is it possible that I created a race condition where 0 is copied before the update if, but it still passes the vba check?

+3
source share
1 answer

Try to calculate your value before using it in VBA:

With Worksheets("analysis").Range("aa5")
    .Calculate
    If .Value <> 0 Then
        Worksheets("export").Range("A1").Value = .Value
    End If
End With
+2
source

All Articles