如何修复在Excel VBA中返回意外结果的函数?

时间:2015-09-12 22:05:15

标签: excel excel-vba vba

我有一个范围 Room_ResultsGUI ,想要创建一个返回值的简单函数。但我的函数没有返回任何内容......不是错误只是空白值,不确定原因。

TasGUID 是带括号的字符串,例如{6086FA55-54E5-4474-8599-2BB696170C73} 我正在检查行和列引用,从第一行和第一行,然后只返回行和列的单元格。 不知道如何解决它,为什么它不起作用?

Function S(TasGUID As Variant) As Variant

    On Error GoTo blad:
    Dim bladTekst As String
    Dim category As String
    Dim zakres As Range

    category = "Total Room Air Flow Rate [l/s]"
    zakres = Range("Room_ResultsGUI")

    Dim kolumna As Long, wiersz As Long
    bladTekst = "No Data"

    kolumna = WorksheetFunction.Match(category, zakres.Rows(1), 0)
    wiersz = WorksheetFunction.Match(TasGUID, zakres.Columns(1), 0)
    S = zakres.Cells(wiersz, kolumna)

    Exit Function
blad:
    S = bladTekst
End Function

我目前的代码:

Function S(TasGUID As Variant) As Variant

    Dim category As String
    Dim zakres As Range
    Dim kolumna As Long, wiersz As Long

    category = "Total Room Air Flow Rate [l/s]"
    kolumna = WorksheetFunction.Match(category, Range("Room_ResultsGUI").Rows(1), 0)
    wiersz = WorksheetFunction.Match(TasGUID, Range("Room_ResultsGUI").Columns(1), 0)
    S = zakres.Cells(wiersz, kolumna)

End Function

1 个答案:

答案 0 :(得分:0)

您需要设置一个Range object

SET zakres = Range("Room_ResultsGUI")

我不太确定无论如何设置它都很重要。使用它,

Function S(TasGUID As Variant) As Variant

    On Error GoTo blad:
    Dim category As String
    Dim kolumna As Long, wiersz As Long

    category = "Total Room Air Flow Rate [l/s]"
    S = "No Data"

    kolumna = WorksheetFunction.Match(category, Range("Room_ResultsGUI").Rows(1), 0)
    wiersz = WorksheetFunction.Match(TasGUID, Range("Room_ResultsGUI").Columns(1), 0)
    S = Range("Room_ResultsGUI").Cells(wiersz, kolumna)

    Exit Function
blad:
    S = Err.Number & " - " & Err.Description

End Function

......就足够了。我还删除了您的通用"无数据"赞成实际的错误编号和描述。