title: Refactoring in VBA - moving parts of code into function does not work
body: <p>I am fairly new to VBA Excel. I am trying to refactor some VBA macro by inserting chunk of code into function and then calling the function instead of the chunk. The chunk of code is copy-pasted many times in the macro (only changing one constant). However the old solution works and the new solution with function does not. It does not even throw an error message, it just freezes the whole Excel application. Do you happen to know what I might be doing wrong?</p>
  
  <p>EDIT: The macro gets called from SAP, if it makes any difference.</p>
  
  <p><strong>OLD:</strong></p>
  
  <pre><code>Dim tbl As Object
  If ThisWorkbook.Container.LinkServer.Items("ITEMS").Table Is Nothing Then
    Exit Sub
  Else
    Set tbl = ThisWorkbook.Container.LinkServer.Items("ITEMS").Table
    maxNumRow = ThisWorkbook.Container.LinkServer.Items("ITEMS").Table.rowCount
    '... (do stuff)
  </code></pre>
  
  <p><strong>NEW:</strong></p>
  
  <pre><code>Dim tbl As Object
  If LinkServer_Table("ITEMS", tbl, maxNumRow) = True Then
    '... (do stuff)
  
  ...
  Function LinkServer_Table( _
    ByVal name As String, _
    ByRef tbl As Object, _
    Optional ByRef rowCount As Long)
  
    If ThisWorkbook.Container.LinkServer.Items(name).Table Is Nothing Then
      LinkServer_Table = False
    Else
      Set tbl = ThisWorkbook.Container.LinkServer.Items(name).Table
      rowCount = ThisWorkbook.Container.LinkServer.Items(name).Table.rowCount
      LinkServer_Table = True
    End If
  End Function
  </code></pre>