我应该为这个 UDF 使用啥对象,它应该根据列标题在一组数据中查找值?

Posted

技术标签:

【中文标题】我应该为这个 UDF 使用啥对象,它应该根据列标题在一组数据中查找值?【英文标题】:What Object should I use for this UDF which is supposed to look in a set of data for the values based on the column header?我应该为这个 UDF 使用什么对象,它应该根据列标题在一组数据中查找值? 【发布时间】:2021-08-03 09:50:37 【问题描述】:

处理 Excel 文档,人们在该文档中将不同工作簿中的工作表内容复制到此文档,以便我的文档可以提取重要信息。问题是他们复制的工作表并不总是具有相同数量的列,并且没有我需要的所有标题。

所以我想编写一个 UDF,它执行以下操作:我将函数放在表的每一列,并指出它应该在复制的数据中查找的标题的名称。找到标题后,在相应的行号上查找写在其下方的值。

这是我目前所写的:

Function AkeneoFind(Req_Header As String) As Range
'Req_Header = The header that the function needs to look for
 
 
Dim Ake_Header As Range     'The range of headers in Akeneo data
Dim Row_Nr As Long          'Row number in which the function is written
Dim Ake_Column As Long      'Column number in Akeneo
Dim Ake_Cell As Range       'The Cell in Akeneo of the respective row and column
Dim Each_Ake As Range       'Each header in the Ake_Header

Set Ake_Header = Sheets("Akeneo").Range("A1:HN1")
Set Row_Nr = Application.ThisCell.Row

For Each Each_Ake In Ake_Column
    If Each_Ake = Req_Header Then
        Ake_Column = Each_Ake.Column
    End If
Next

Ake_Cell = Cell(Row_Nr, Ake_Column)
If Ake_Cell <> "" Then
    AkeneoFind = Ake_Cell
    Else
    AkeneoFind = "Error"
End If

'Insert value for header. Look among all headers in Akeneo. Once it has 
'found it, it will look up based on the row number that the Function is in 
'whether the cell is empty or not. If it isn't, then return that value.

End Function

现在这里的问题是第一行缺少一个对象,但是如果还有其他问题我也不会感到惊讶,所以请有人帮忙吗?

编辑: 更新了 UDF,现在它不再弹出错误。然而,它返回的值是#VALUE,即使我写了正确的标题(带或不带“”)。

Function AkeneoFind(Req_Header As String) As String
'Req_Header = The header that the function needs to look for
 
 
Dim Ake_Header As Range     'The range of headers in Akeneo Invoer
Dim Row_Nr As Long          'Row number in which the function is written
Dim Ake_Column As Long      'Column number in Akeneo Invoer
Dim Ake_Cell As Range       'The Cell in Akeneo of the respective row and column
Dim Each_Ake As Range       'Each header in the Ake_Header

Set Ake_Header = Sheets("Akeneo").Range("A1:HN1")
Row_Nr = Application.ThisCell.Row

For Each Each_Ake In Ake_Header
    If Each_Ake = Req_Header Then
        Ake_Column = Each_Ake.Column
    End If
Next

Ake_Cell = Cells(Row_Nr, Ake_Column)
If Ake_Cell <> "" Then
    AkeneoFind = Ake_Cell
    Else
    AkeneoFind = "Error"
End If

'Insert value for header. Look among all headers in Akeneo. Once it has 
'found it, it will look up based on the row number that the Function is in 
'whether the cell is empty or not. If it isn't, then return that value.

End Function

【问题讨论】:

Set Row_Nr = Application.ThisCell.Row - 您将Row_Nr 声明为Long,因此您不需要使用Set 语句,只需Row_Nr = Application.ThisCell.Row 即可(另外,Row 属性返回一个Long,不是对象) @RaymondWu "所以你不需要使用Set"不是100%正确:你不能使用Set @FunThomas - 确实,您不能将Set 用于非对象变量类型。 【参考方案1】:

试试这个:

Function AkeneoFind(Req_Header As String) As String
    'Req_Header = The header that the function needs to look for
         
    Dim Ake_Header As Range     'The range of headers in Akeneo data
    Dim Row_Nr As Long          'Row number in which the function is written
    Dim Ake_Column As Long      'Column number in Akeneo
    Dim Ake_Cell As String       'The Cell in Akeneo of the respective row and column
    Dim Each_Ake As Range       'Each header in the Ake_Header

    Set Ake_Header = Sheets("Akeneo").Range("A1:HN1")
    Row_Nr = Application.ThisCell.Row
    
    For Each Each_Ake In Ake_Header
        If Each_Ake = Req_Header Then
            Ake_Column = Each_Ake.Column
            Exit For
        End If
    Next

    Ake_Cell = Sheets("Akeneo").Cells(Row_Nr, Ake_Column).Value
    
    If Ake_Cell <> vbNullString Then
        AkeneoFind = Ake_Cell
    Else
        AkeneoFind = "Error"
    End If

    'Insert value for header. Look among all headers in Akeneo. Once it has found it, it will look up based on the row number that the Function is in whether the cell is empty or not. If it isn't, then return that value.

End Function

【讨论】:

这样它会给出错误:“有一个或多个循环引用,其中公式直接或间接引用其自己的单元格。这可能会导致它们计算不正确。尝试删除或更改这些引用,或将公式移动到不同的单元格。"。公式出来的值也变成了0。 我已经更新了代码,再试一次@AevirDenken 做到了!非常感谢@Raymond Wu!

以上是关于我应该为这个 UDF 使用啥对象,它应该根据列标题在一组数据中查找值?的主要内容,如果未能解决你的问题,请参考以下文章

在将 .toArray() 用于 Spark 向量之后,它应该是啥类型?

UDF 的输入类型应该是啥类型的列 - StructType 数组或“null”?

Spark DataFrame UDF 分区列

应该将 JWT 中不存在的角色添加到 Authentication 对象的啥位置?

python 类中的对象声明是啥意思,我应该使用它吗? [复制]

为 Spark UDF 执行提供上下文