【问题标题】:Excel VBA User-Defined Function: Get Cell in Sheet Function was Called InExcel VBA 用户定义函数:在工作表函数中获取单元格被调用
【发布时间】:2015-06-23 16:08:27
【问题描述】:

我在 Excel 中有一个用户定义的函数,我在多个工作表中运行。我正在尝试使用Cells(i, j) 在调用我的函数的工作表中通过它们的行和列提取单元格的值。相反,当我点击“立即计算”按钮时,Cells(i, j) 会在 活动工作表 中提取单元格 [i,j] 的值,但自动计算不起作用。

我做错了什么?

完整功能如下,不知道是否需要回答我的问题。

Option Explicit
Option Base 1
Option Compare Text

Function recordString(ByVal width As Integer, ByVal height As Integer, ByVal firstCell As Range) As String
    Application.Volatile

    Dim tempString As String
    tempString = ""
    Dim i As Integer
    Dim j As Integer
    For i = firstCell.Row To firstCell.Row + height - 1
        For j = firstCell.Column To firstCell.Column + width - 1
            If IsEmpty(Cells(i, j)) Then
                tempString = tempString & "0"
            Else
                tempString = tempString & "1"
            End If
        Next j
    Next i
    recordString = tempString
End Function

【问题讨论】:

    标签: vba excel user-defined-functions


    【解决方案1】:

    你需要使用 Application.Caller。

    这将返回输入函数的工作表的单元格 A1 中的值:

    Public Function DisplayCaller() As String
    
        DisplayCaller = Application.Caller.Parent.Cells(1, 1)
    
    End Function
    

    这将返回调用表的名称:

    Public Function DisplayCaller() As String
    
        DisplayCaller = Application.Caller.Parent.Name
    
    End Function
    

    欲了解更多信息:
    http://www.cpearson.com/excel/sheetref.htm

    【讨论】:

    • 完美,谢谢。这就是我认为 Cells 函数可以直观地工作的原因。
    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2011-06-06
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多