title: How does the VBA immediate window differ from the application runtime?
body: <p>I've encountered a very strange bug in VBA and wondered if anyone could shed some light?</p>
  
  <p>I'm calling a worksheet function like this:  </p>
  
  <pre><code>Dim lMyRow As Long
  lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
  </code></pre>
  
  <p>This is intended to get the row of the item I pass in. Under certain circumstances (although I can't pin down exactly when), odd things happen to the call to the Match function.</p>
  
  <p>If I execute that line in the immediate window, I get the following:</p>
  
  <pre><code>lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
  ?lMyRow
  10
  </code></pre>
  
  <p>i.e. the lookup works, and lMyRow gets a value assigned to it. If I let that statement execute in the actual code, I lMyRow gets a value of 0.</p>
  
  <p>This seems very odd! I don't understand how executing something in the immediate window can succeed in assigning a value, where the same call, at the same point in program execution can give a value of 0 when it runs normally in code!</p>
  
  <p>The only thing I can think of is that it's some odd casting thing, but I get the same behaviour taking if the variable to which I'm assigning is an int, a double, or even a string.</p>
  
  <p>I don't even know where to begin with this - help!!</p>