title: Application-defined or object-defined error for if block
body: <p>I have written the below piece of code in VBA that basically checks if the value  in cell B24 is positive and if positive it iterates through a for-loop for 50 times where X0 and sqrt are repeatedly updated.</p>
  <p>However when I debug the code it throws an error</p>
  <pre><code>'Run-time error '1004'
    Application-defined or object-defined error.
  </code></pre>
  <p>I checked the syntax, it looks fine but the error keeps popping. How to resolve this issue? Help pls</p>
  <pre><code>Sub forloopCalc()
  
  Dim X0, sqrt As Integer
  Dim sheetName As String
      sheetName = &quot;Sheet1&quot;
      Dim WSD As Worksheet
      Set WSD = Worksheets(sheetName)
      
  
  If (Cells(B, 24).Value &gt; 0) Then
      X0 = 5.96046447753906E-08
      sqrt = 0.000244140625
      For i = 0 To 50
          If ((X0 - Cells(B, 24).Value) &lt; 0) Then
              X0 = X0 * 4
              sqrt = sqrt * 2
          Exit For
          End If
      Next i
  End If
  
  WSD.Cells(P, 24).Value = X0
  WSD.Cells(R, 24).Value = sqrt
       
  
  End Sub
  </code></pre>