使用UDF将范围返回为字符串以在查找函数中使用的方法

时间:2019-02-26 18:25:34

标签: excel vba excel-formula

对于我的问题,我有一个UDF,它接受一个字符串,并使用select大小写,然后将字符串引用返回到指定的数组。返回字符串是对工作表和范围数组的引用,格式如下:

  

'WorksheetExample'!$ B $ 1:$ E $ 44

此UDF嵌套在我的表的VLookup / Match中,以为该情况提供正确的参考。

Public Function EquipCatRNG(tag As String, tagOrCatRef As String)

'Hardcoded Category Ranges for Matches
Dim tagRef, catRef As String
Dim subfix As String
subfix = Left(tag, 1)
Select Case subfix
    Case "E"
    'Range for heat exchanger info, 'Heat Exchangers Template (EDS)'!$B$1:$J$53
    tagRef = "'Heat Exchangers Template (EDS)'!$B$1:$J$1"
    catRef = "'Heat Exchangers Template (EDS)'!$B$1:$B$53"
    Case "P"
        If InStr(tag, "Drive") > 0 Then
        tagRef = "'Pumps And Drives Template (EDS)'!$B$44:$G$44"
        catRef = "'Pumps And Drives Template (EDS)'!$B$44:$B$68"
        Else
        tagRef = "'Pumps And Drives Template (EDS)'!$B$1:$G$1"
        catRef = "'Pumps And Drives Template (EDS)'!$B$1:$B$42"
        End If
    Case "T"
        If InStr(tag, "Internals") > 0 Then
        tagRef = "'Towers Template (EDS)'!$B$46:$E$46"
        catRef = "'Towers Template (EDS)'!$B$46:$B$68"
        Else
        tagRef = "'Towers Template (EDS)'!$B$1:$E$1"
        catRef = "'Towers Template (EDS)'!$B$1:$B$44"
        End If
    Case "R"
        tagRef = "'Reactors Template (EDS)'!$B$1:$C$1"
        catRef = "'Reactors Template (EDS)'!$B$1:$B$61"
    Case "H"
        tagRef = "'Heaters Template (EDS)'!$B$1:$C$1"
        catRef = "'Heaters Template (EDS)'!$B$1:$B$45"
    Case "V"
        If InStr(tag, "Internals") > 0 Then
        tagRef = "'Vessels Template (EDS)'!$B$45:$F$45"
        catRef = "'Vessels Template (EDS)'!$B$45:$B$64"
        Else
        tagRef = "'Vessels Template (EDS)'!$B$1:$F$1"
        catRef = "'Vessels Template (EDS)'!$B$1:$B$43"
        End If
End Select
    tagOrCatRef = LCase(tagOrCatRef)
    If tagOrCatRef = "tag" Then
        EquipCatRNG = tagRef
    Else
        EquipCatRNG = catRef
    End If

End Function

任何帮助,我都很好,直接从UDF传递Range而不是返回字符串,然后执行一些操作以在公式中利用它,也可以。谢谢。

为清晰起见,请编辑: 此函数成功传递了String,但在我的表的Vlookup / Match函数中不能作为有效的引用。我需要知道我如何: 1.对返回的字符串post-UDF执行公式运算,以便Vlookup接受它作为有效引用? 2.最初将其作为范围传递吗?

1 个答案:

答案 0 :(得分:2)

要在另一个工作表函数中使用“在线” UDF,需要将UDF设置为该范围。

Public Function EquipCatRNG(tag As String, tagOrCatRef As String) as range

'Hardcoded Category Ranges for Matches
Dim tagRef As range, catRef As range
Dim subfix As String
subfix = Left(tag, 1)
Select Case subfix
    Case "E"
        'Range for heat exchanger info, 'Heat Exchangers Template (EDS)'!$B$1:$J$53
        Set tagRef = worksheets("Heat Exchangers Template (EDS)").Range("B1:J1")
        Set catRef = worksheets("Heat Exchangers Template (EDS)").Range("B1:B53")
    Case "P"
        If InStr(tag, "Drive") > 0 Then
          Set tagRef = worksheets("Pumps And Drives Template (EDS)").Range("B44:G44")
          Set catRef = worksheets("Pumps And Drives Template (EDS)").Range("B44:B68")
        Else
          Set tagRef = worksheets("Pumps And Drives Template (EDS)").Range("B1:G1")
          Set catRef = worksheets("Pumps And Drives Template (EDS)").Range("B1:B42")
        End If
    Case "T"
        'etc, etc, etc
         ...

End Select

    tagOrCatRef = LCase(tagOrCatRef)
    If tagOrCatRef = "tag" Then
        Set EquipCatRNG = tagRef
    Else
        Set EquipCatRNG = catRef
    End If

End Function

在工作表上使用,看起来像

=MATCH("abc", EquipCatRNG("E", "Tag"), 0)