从单元调用函数时会返回错误的值
问题描述:
首先发布yall。从单元调用函数时会返回错误的值
长话短说,在Excel中,当我从单元中调用以下函数(它在它自己的模块中)时,它返回错误的值。当从一个子对象调用它时,以及当我遍历代码(到最后)时,该函数返回正确的值,但是当我从Excel调用它时,它返回一个不同的值。在底部的背景。
事情我已经试过
- 使其成为一个公共职能
- 给它一个说法
- 更改功能和/或模块名称
- 移动出来的模块 的
- 重新启动Excel
- 一堆随机东西
这真的只是这个特定的功能给我这个问题,更简单的功能做他们被告知。我必须假定它与Excel正在做的事情的顺序有关,或者Excel的某些功能可以改变的限制。
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 <> 0 Then
uniquediscstring = uniquediscstring & ", " & cell.Value
Else
uniquediscstring = cell.Value
i = 1
End If
End If
Next
ActiveDisciplineFilters = uniquediscstring
End Function
背景
在Excel中,我有一个表。我将所有数据都放在该表的一个特定列中,并在该范围内创建一个唯一值的字符串(用逗号分隔)。该字符串必须放置在另一个单元格中,原因我不需要考虑。如果过滤器应用于列,则唯一值会自动更新。
当我从一个子对象调用它时,Excel会给我正确的答案,当我从一个单元调用它时,会出现错误的答案。
答
不幸的是,没有一个SpecialCells方法在UDF中工作。如果你需要从工作表中以公式的形式运行它,那么你的代码应该看起来像这样:
Function ActiveDisciplineFilters()
Application.Volatile 'makes the function update automatically
Dim disccolumn As Range
Dim uniquedisc() As String
Dim uniquediscstring As String
Dim i As Long
Dim cell As Range
Dim bHidden As Boolean
'create a string of unique values from the Discipline column
i = 0
For Each cell In Range("LayerList[Discipline]").Cells
If cell.EntireRow.Hidden = False Then
If InStr(1, uniquediscstring, cell.Value) = 0 Then
If i <> 0 Then
uniquediscstring = uniquediscstring & ", " & cell.Value
Else
uniquediscstring = cell.Value
i = 1
End If
End If
Else
bHidden = True
End If
Next
If Not bHidden Then uniquediscstring = "None"
ActiveDisciplineFilters = uniquediscstring
End Function
谢谢你亲切的互联网人。这正是我所希望的。 –