Excel 自定义函数不能跨工作表工作
Excel custom function does not work across sheets
我对编写 excel 自定义函数很陌生,我遇到了一种奇怪的行为,我在网上搜索时似乎找不到解释(当然信息量很大) ).请多多包涵。
这里有一个演示,它至少显示了部分问题:
Function mycustomfn_demo(rng As Range)
Dim rngrows, rngcols, curcol, currow, i, j,firstcol As Integer
Dim exitflag As Boolean
firstcol = -1
rngrows = rng.Rows.Count
rngcols = rng.Columns.Count
exitflag = False
For i = 1 To rngcols
For j = 1 To rngrows
curcol = rng.Column + i - 1
currow = rng.Row + j - 1
If Cells(currow, curcol).Value <> "" Then
firstcol = i
exitflag = True
Exit For
End If
Next j
If exitflag = True Then
Exit For
End If
Next i
mycustomfn_demo = firstcol
End Function
此函数查找具有非空白单元格的范围内的第一列(范围内没有非空白单元格的结果为 -1)并且只要范围 rng
在相同 sheet 作为包含 mycustomfn_demo
函数的公式。这是包含公式和范围的 Sheet 1:
但是,如果它们在不同的 sheets 上,就会发生奇怪的事情这表明 Sheet 2(范围仍在 Sheet 1):
在这种情况下(但在其他情况下不会)引用 Sheet 1 中的公式单元格给出正确的结果(同样,Sheet 2):
这是预期的行为,还是错误的结果?我在 OSX High Sierra 10.13.5 下为 Mac 使用 Office 2016,Excel 版本为 15.23。
我应该补充一点,在更复杂的情况下,引用来自另一个 sheet 的自定义公式结果会从公式单元格本身中删除结果。然后可以通过删除该单元格然后选择撤消来恢复它。
问题出在下面,
If Cells(currow, curcol).Value <> "" Then
单元格没有父工作sheet 引用,因此尽管您从 Sheet1 上的单元格引用传递行号和列号,它只是使用这些数字在活动 sheet.
添加对 rng 父作品的引用sheet。
with rng.parent
For i = 1 To rngcols
For j = 1 To rngrows
curcol = rng.Column + i - 1
currow = rng.Row + j - 1
If .Cells(currow, curcol).Value <> "" Then
firstcol = i
exitflag = True
Exit For
End If
Next j
If exitflag = True Then
Exit For
End If
Next i
end with
请注意,Cells 变为 .Cells 以捕获 With ... End With 块中的父工作sheet 引用。
仅使用一个可能很容易成为 rng.parent.Cells(currow, curcol).Value
的引用,但 With ... End With 块更彻底地扩展了对 rng 工作中其他单元格的调用sheet。
我对编写 excel 自定义函数很陌生,我遇到了一种奇怪的行为,我在网上搜索时似乎找不到解释(当然信息量很大) ).请多多包涵。
这里有一个演示,它至少显示了部分问题:
Function mycustomfn_demo(rng As Range)
Dim rngrows, rngcols, curcol, currow, i, j,firstcol As Integer
Dim exitflag As Boolean
firstcol = -1
rngrows = rng.Rows.Count
rngcols = rng.Columns.Count
exitflag = False
For i = 1 To rngcols
For j = 1 To rngrows
curcol = rng.Column + i - 1
currow = rng.Row + j - 1
If Cells(currow, curcol).Value <> "" Then
firstcol = i
exitflag = True
Exit For
End If
Next j
If exitflag = True Then
Exit For
End If
Next i
mycustomfn_demo = firstcol
End Function
此函数查找具有非空白单元格的范围内的第一列(范围内没有非空白单元格的结果为 -1)并且只要范围 rng
在相同 sheet 作为包含 mycustomfn_demo
函数的公式。这是包含公式和范围的 Sheet 1:
但是,如果它们在不同的 sheets 上,就会发生奇怪的事情这表明 Sheet 2(范围仍在 Sheet 1):
在这种情况下(但在其他情况下不会)引用 Sheet 1 中的公式单元格给出正确的结果(同样,Sheet 2):
这是预期的行为,还是错误的结果?我在 OSX High Sierra 10.13.5 下为 Mac 使用 Office 2016,Excel 版本为 15.23。
我应该补充一点,在更复杂的情况下,引用来自另一个 sheet 的自定义公式结果会从公式单元格本身中删除结果。然后可以通过删除该单元格然后选择撤消来恢复它。
问题出在下面,
If Cells(currow, curcol).Value <> "" Then
单元格没有父工作sheet 引用,因此尽管您从 Sheet1 上的单元格引用传递行号和列号,它只是使用这些数字在活动 sheet.
添加对 rng 父作品的引用sheet。
with rng.parent
For i = 1 To rngcols
For j = 1 To rngrows
curcol = rng.Column + i - 1
currow = rng.Row + j - 1
If .Cells(currow, curcol).Value <> "" Then
firstcol = i
exitflag = True
Exit For
End If
Next j
If exitflag = True Then
Exit For
End If
Next i
end with
请注意,Cells 变为 .Cells 以捕获 With ... End With 块中的父工作sheet 引用。
仅使用一个可能很容易成为 rng.parent.Cells(currow, curcol).Value
的引用,但 With ... End With 块更彻底地扩展了对 rng 工作中其他单元格的调用sheet。