引用 UDF 中的特定单元格
Posted
技术标签:
【中文标题】引用 UDF 中的特定单元格【英文标题】:Referring to a specific cell in UDF 【发布时间】:2018-08-21 07:40:06 【问题描述】:UDF 目标:将范围的值与同一工作表上的特定单元格进行比较。
发生错误#Value!
。
我认为,将通行证设置为此单元格ThisWorkbook.ThisWorksheet
的问题。如何胜任?
Function Fav(Diapozon As Range) As Long
Application.Volatile
Dim n As Long
For x = 1 To 4
For y = 0 To 1
If Diapozon.Value = ThisWorkbook.Thisworksheet.Cells(x + 29, y + 10).Value Or _
Diapozon.Offset(0, 1).Value = ThisWorkbook.Thisworksheet.Cells(x + 29, y + 10).Value Then
n = 1
End If
Next y
Next x
Fav = n
End Function
【问题讨论】:
【参考方案1】:正确。也许您指的是 Activesheet?
Public Function Fav(ByVal Diapozon As Range) As Long
Application.Volatile
Dim n As Long, x As Long, y As Long
For x = 1 To 4
For y = 0 To 1
If Diapozon.Value = ThisWorkbook.ActiveSheet.Cells(x + 29, y + 10).Value Or Diapozon.Offset(0, 1).Value = ThisWorkbook.ActiveSheet.Cells(x + 29, y + 10).Value Then
n = 1
End If
Next y
Next x
Fav = n
End Function
如果您仅在工作表中将其用作 UDF,则删除工作表引用:
Public Function Fav(ByVal Diapozon As Range) As Long
Application.Volatile
Dim n As Long, x As Long, y As Long
For x = 1 To 4
For y = 0 To 1
If Diapozon.Value = Cells(x + 29, y + 10).Value Or Diapozon.Offset(0, 1).Value = Cells(x + 29, y + 10).Value Then
n = 1
End If
Next y
Next x
Fav = n
End Function
【讨论】:
以上是关于引用 UDF 中的特定单元格的主要内容,如果未能解决你的问题,请参考以下文章