I'm probably missing something simple, but ListRows.Add is giving me grief. Here's the function:
Sub addEmployee(employeeName As String, tableToAddTo As ListObject)
Dim newRow As ListRow
Set newRow = tableToAddTo.ListRows.Add()
newRow.Range.Cells(1, 1).Value = employeeName
tableToAddTo.Sort.Apply
End Sub
In most cases, this works fine. However, whenever the function runs on a certain table in my worksheet, the lines following the call to ListRows.Add are never executed (at least that's what the debugger indicates) and the row does not get added to the table. Any thoughts/ideas?
UPDATE:
Here's what I've learned since the post. If I feed the Sub with stub data it works fine. For example:
Sub driver()
Dim myTable As ListObject
Set myTable = getTableObject("myTableName")
Call addEmployee("myName", myTable)
End Sub
Note: getTableObject cycles through the worksheets and returns the ListObject with the matching name.
It seems to be an issue dealing with the context in which the code is called. In the case that fails, a formula (Function call) has been placed in various cells of various worksheets. The formula contains references to data in other cells. When the data in the other cells changes, the formula is invoked, which in turn calls the addEmployee Sub that is given above. This is the case that fails.