title: Wrong value is returned when calling a function from a cell
body: <p>First post yall. </p>
  
  <p>Long story short, in Excel, when I call the following function (it's in its own module) from a cell, it returns the wrong value.  The function returns the correct value when calling it from a sub, as well as when I step through the code (to the end), but the moment I call it from Excel, it returns a different value. Background at the bottom.</p>
  
  <p><strong>Things I've Tried</strong>  </p>
  
  <ul>
  <li>Making it a Public Function</li>
  <li>Giving it an argument</li>
  <li>Changing the function and/or module name</li>
  <li>Moving it out of a module</li>
  <li>Restarting Excel</li>
  <li>A bunch of random stuff</li>
  </ul>
  
  <p>It really is just this specific function that's giving me this issue, simpler functions do what they're told. I have to assume it has something to do with the order of events Excel is doing things, or the limits of what parts of Excel a function can change. </p>
  
  <pre><code>Function ActiveDisciplineFilters()
  
      Application.Volatile 'makes the function update automatically
      Dim disccolumn As Range
      Dim uniquedisc() As String
      Dim uniquediscstring As String
  
      'create a string of unique values from the Discipline column
      i = 0
      If Range("LayerList[Discipline]").SpecialCells(xlCellTypeVisible).Address = Range("LayerList[Discipline]").Address Then
          ActiveDisciplineFilters = "None"
          Exit Function
      End If
  
      For Each cell In Range("LayerList[Discipline]").SpecialCells(xlCellTypeVisible)
          If InStr(1, uniquediscstring, cell.Value) = 0 Then
              If i &lt;&gt; 0 Then
                  uniquediscstring = uniquediscstring &amp; ", " &amp; cell.Value
              Else
                  uniquediscstring = cell.Value
                  i = 1
              End If
          End If
      Next
  
      ActiveDisciplineFilters = uniquediscstring
  
  End Function
  </code></pre>
  
  <p><strong>Background</strong></p>
  
  <p>In Excel, I have a table. I'm taking all the data in one specific column of that table and creating a string of the unique values in that range (separated by comma). That string must be placed in another cell, for reasons I don't need to get into. If a filter is applied to the column, the unique values update automatically.</p>
  
  <p><em>What would make Excel give me the right answer when I call it from a sub, then the wrong one when I call it from a cell?</em></p>