Evaluate Formula From Another Sheet as Current Sheet
<p>I'm trying to use Eval() to take some functions stored on Main and evaluate them with values from Sheet2. So far static references to Main and pure text work, but I'm having trouble referencing relative cells.</p>

<p>Ideally, the Main worksheet combines static and relative references and applies the function down the column to rows in a table (such that it's A7, A8, ... in this example). The single quotes are for forming SQL. Here is one of the functions I'm trying to implement.</p>

<pre><code>=CONCATENATE(Main!$B$30, "'", A7, "', '", B7, "', '", C7, "', '", D7, "', '", E7, "', '",F7, "', '",G7, "', '",H7, "', '",I7, "', '",J7, "', '", K7, "', ",$B$4,Main!$B$35)
</code></pre>

<p>I think the problem is in my Eval function, it's not evaluating A7 as a cell value in the active sheet. I was looking into a way to change the function to something like CurrentSheet!A7, but couldn't find the proper syntax.</p>

<pre><code>Function Eval(Ref As Range)
   Dim shCurrent As Worksheet: Set shCurrent = ActiveSheet
   Application.Volatile
   Application.ThisCell.Parent.Activate
   Eval = Application.ThisCell.Parent.Evaluate(Ref.Formula)
   shCurrent.Activate
End Function
</code></pre>

<p>I'm referencing this Eval function in Sheet2 like this, copied down my table column where Main!$E$5 is a cell that has the above excel function in it.</p>

<pre><code>=EVAL(Main!$E$5)
</code></pre>

<p><strong>ANSWER</strong></p>

<p>I took bwyn's example and modified it for my use. This is what the above excel function ended up looking like as a VB function. Sending in the row number as a parameter makes the calling function the most generic, allowing the spreadsheet master to extend the "A" and "K" values as needed in one location.</p>

<pre><code>Function EvalInsert(rowNum As Integer)
  Dim shCurrent As Worksheet: Set shCurrent = ActiveSheet
  Dim shMain As Worksheet: Set shMain = Sheets("Main")
  Dim insertClose As String: insertClose = "'));"
  Dim openParen As String: openParen = "'"
  Dim closeParen As String: closeParen = "', "
  Application.Volatile

  Set insertOpen = shMain.Range("B30")
  Set tableId = shMain.Range("B33")
  Set TableName = shCurrent.Range("B1")
  Set insertValues = shCurrent.Range(Cells(rowNum, "A"), Cells(rowNum, "K"))

  EvalInsert = insertOpen.Value

  For Each c In insertValues.Cells
     EvalInsert = EvalInsert &amp; openParen &amp; c.Value &amp; closeParen
  Next

  EvalInsert = EvalInsert &amp; tableId &amp; TableName &amp; insertClose

End Function
</code></pre>

<p>I called this using</p>

<pre><code>=EvalInsert(ROW())
</code></pre>