我应该为这个 UDF 使用什么 Object,它应该在一组数据中查找基于列 header 的值?

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?

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

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

这是我到目前为止写的:

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

现在的问题是第一行少了一个object,但如果还有其他问题我也不会感到惊讶,所以有人可以帮忙吗?

编辑: 更新了 UDF,现在它不再弹出错误。但是,它 returns 的值是#VALUE,即使我写的是正确的 header (带或不带“”)。

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

试试这个:

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