title: Reference an Excel Sheet Name with a variable
body: <p>I have a function:</p>
  <pre><code>Public Function getLastRow(sheetName As String) As Integer
      getLastRow = Worksheets(sheetName).Cells(Rows.count, 1).End(xlUp).row
  End Function
  </code></pre>
  <p>When I try to use that function . . .</p>
  <pre><code>Sub test()
  MsgBox getLastRow(&quot;Data&quot;)
  End Sub
  </code></pre>
  <p>. . . I get a subscript out of Range (run-time error '9') error. How can I make the error go away?</p>
  <p>running <code>getLastRow = Worksheets(&quot;Data&quot;).Cells(Rows.count, 1).End(xlUp).row</code> works fine. But I want to be able to dynamically choose the sheet name in my function.</p>