[英]How to fix function that is returning unexpected results in Excel 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
您需要设置一个Range对象 。
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
...就足够了。 我还删除了您的通用“无数据”,以提供实际的错误编号和描述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.