title: Calling Excel Macro from C# throwing Compile Error : Sub Or Function not defined
body: <p>I am creating a web application to replace the current Excel Macro Application. The Excel macro has several forms and modules in it. When I call a macro function in the C# using xlApp.Run method, the function is getting called but throwing error when the function calls other sub functions and uses global public variables. </p>
  
  <p>Errors : 
  1. Compile Error : Sub Or Function not defined - at "Set svd = m_queries(GetSheetRootName(Sheet1))"
  2. Runtime Error 424 : Object Required - at "CompareWorksheetsToCombined"</p>
  
  <p>Code : </p>
  
  <pre><code>Public Function CompareExcels(filePath1 As String, filePath2 As String)
  Call MsgBox("Done comparing sheets1.", vbInformation, "Compare Complete")
  On Error GoTo CompErr
  Call MsgBox("Done comparing sheets2.", vbInformation, "Compare Complete")
  Dim compBook As Workbook
  Dim book1 As Workbook, book2 As Workbook
  Dim sheet, Sheet1 As Worksheet, Sheet2 As Worksheet
  Dim svd As ScriptViewDescription, obj
  Dim l As Long
  Dim sheetName As String
  Dim a As Integer
  
  '// Check to make sure we have valid options selected for compare
  If ValidateOptions = False Then Exit Function
  
  cmdCompare.Enabled = True
  
  '// Create the comparison output workbook
  Set compBook = OpenCompareOutput()
  
  '// Get the workbooks we're comparing
  Set book1 = Workbooks(filePath1)
  Set book2 = Workbooks(filePath2)
  
  '// Verify if the Summary Sheet Exists and compare the libraries.
  '//If (WorksheetExists("[" &amp; filePath1 &amp; "]Summary")) And (WorksheetExists("[" &amp; filePath2 &amp; "]Summary")) Then
  
      Set Sheet1 = book1.Sheets("Summary")
      Set Sheet2 = book2.Sheets("Summary")
  
      '//If Sheet1.Cells(2, 3) = Sheet2.Cells(2, 3) Then
         '// a = MsgBox("Attention: You are comparing the data from Same Environment", vbCritical, "Critical Warning")
      '//End If
  '//Else
      '//a = MsgBox("Summary Tab does not exist in either Workbook A or B and the Libraries are not compared", vbCritical, "Critical Warning")
  '//End If
  
  '// Look for selected sheets and then compare them
  For l = 0 To lstSheets.ListCount - 1
      If lstSheets.selected(l) = True Then
          '// reset vars to make sure we don't accidentally re-use from last loop
          Set Sheet1 = Nothing
          Set Sheet2 = Nothing
          Set svd = Nothing
  
          '// Attempt to load up the vars with the new stuff
          sheetName = lstSheets.list(l)
          Set Sheet1 = book1.Sheets(sheetName)
          Set Sheet2 = book2.Sheets(sheetName)
          Set svd = m_queries(GetSheetRootName(Sheet1))
  
          '// Check vars and if we're good then compare
          If Not (Sheet1 Is Nothing) _
          And Not (Sheet2 Is Nothing) _
          And Not (svd Is Nothing) Then
              Call CompareWorksheetsToCombined(Sheet1, Sheet2, compBook, svd, txtPrefixA.Text, txtPrefixB.Text)
          End If
      End If
  Next l
  
  cmdCompare.Enabled = True
  Call MsgBox("Done comparing sheets.", vbInformation, "Compare Complete")
  
  Exit Function
  CompErr:
  Call MsgBox("Error while attempting to process compares." &amp; vbCrLf &amp; vbCrLf &amp; "Error " &amp; Err.Number &amp; ": " &amp; Err.Description, vbCritical, "Error During Compare")
  cmdCompare.Enabled = True
  End Function
  </code></pre>
  
  <p>Please help out.</p>