UDF 在 VBA 中调用正常,但在工作表中调用时返回空白
UDF calls fine in VBA, but returning blanks when called in worksheet
我在模块中有一个函数,在 Sub 中调用时运行良好,但在工作表中使用时,除了空白之外什么也得不到。这些功能意味着有 3 个输入。一个输入确定要检查的工作表,然后找到与其他 2 个条件匹配的行。我使用了一个调用该函数的 sub,并提供了一个带有结果的 msgbox,这是正确的。但在工作表中,相同的输入导致一个空白单元格。
Function ToolStatus(PartNumber As String, Model As String, Number As Integer) As String
Dim SearchSheet As Worksheet
Dim PN As Integer
Dim MdlCol As Integer
Dim Mdl As String
Dim Result As Integer
Dim FinalRow As Integer
Dim i As Integer
Application.ScreenUpdating = False
Select Case True
Case Number < WorksheetFunction.CountA(Sheet2.Range("B:B")) And Model = "1A"
Set SearchSheet = Sheet2
PN = 2
MdlCol = 4
Mdl = "1A"
Result = 19
Case Number < WorksheetFunction.CountA(Sheet2.Range("B:B")) And Model = "1B"
Set SearchSheet = Sheet2
PN = 2
MdlCol = 5
Mdl = "1B"
Result = 19
Case Number < WorksheetFunction.CountA(Sheet2.Range("B:B")) And Model = "1C"
Set SearchSheet = Sheet2
PN = 2
MdlCol = 6
Mdl = "1C"
Result = 19
Case Number < WorksheetFunction.CountA(Sheet3.Range("B:B")) And Model = "1A"
Set SearchSheet = Sheet3
PN = 3
MdlCol = 17
Mdl = "-1A"
Result = 4
Case Number < WorksheetFunction.CountA(Sheet3.Range("B:B")) And Model = "1B"
Set SearchSheet = Sheet3
PN = 3
MdlCol = 18
Mdl = "-1B"
Result = 4
Case Number < WorksheetFunction.CountA(Sheet3.Range("B:B")) And Model = "1C"
Set SearchSheet = Sheet3
PN = 3
MdlCol = 19
Mdl = "-1C"
Result = 4
End Select
SearchSheet.Select
FinalRow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To FinalRow
If Cells(i, PN) = PartNumber And Cells(i, MdlCol) = Mdl Then
ToolStatus = Cells(i, Result).Value
Exit For
End If
Next i
Application.ScreenUpdating = True
End Function
SearchSheet.Select
您不能在称为 UDF 的函数中执行此操作。
有关详细信息,请参阅 https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel。
虽然您不需要 select sheet - 只需使用
FinalRow = SearchSheet.Cells(Rows.Count, 2).End(xlUp).Row
(和以下代码类似)
我在模块中有一个函数,在 Sub 中调用时运行良好,但在工作表中使用时,除了空白之外什么也得不到。这些功能意味着有 3 个输入。一个输入确定要检查的工作表,然后找到与其他 2 个条件匹配的行。我使用了一个调用该函数的 sub,并提供了一个带有结果的 msgbox,这是正确的。但在工作表中,相同的输入导致一个空白单元格。
Function ToolStatus(PartNumber As String, Model As String, Number As Integer) As String
Dim SearchSheet As Worksheet
Dim PN As Integer
Dim MdlCol As Integer
Dim Mdl As String
Dim Result As Integer
Dim FinalRow As Integer
Dim i As Integer
Application.ScreenUpdating = False
Select Case True
Case Number < WorksheetFunction.CountA(Sheet2.Range("B:B")) And Model = "1A"
Set SearchSheet = Sheet2
PN = 2
MdlCol = 4
Mdl = "1A"
Result = 19
Case Number < WorksheetFunction.CountA(Sheet2.Range("B:B")) And Model = "1B"
Set SearchSheet = Sheet2
PN = 2
MdlCol = 5
Mdl = "1B"
Result = 19
Case Number < WorksheetFunction.CountA(Sheet2.Range("B:B")) And Model = "1C"
Set SearchSheet = Sheet2
PN = 2
MdlCol = 6
Mdl = "1C"
Result = 19
Case Number < WorksheetFunction.CountA(Sheet3.Range("B:B")) And Model = "1A"
Set SearchSheet = Sheet3
PN = 3
MdlCol = 17
Mdl = "-1A"
Result = 4
Case Number < WorksheetFunction.CountA(Sheet3.Range("B:B")) And Model = "1B"
Set SearchSheet = Sheet3
PN = 3
MdlCol = 18
Mdl = "-1B"
Result = 4
Case Number < WorksheetFunction.CountA(Sheet3.Range("B:B")) And Model = "1C"
Set SearchSheet = Sheet3
PN = 3
MdlCol = 19
Mdl = "-1C"
Result = 4
End Select
SearchSheet.Select
FinalRow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To FinalRow
If Cells(i, PN) = PartNumber And Cells(i, MdlCol) = Mdl Then
ToolStatus = Cells(i, Result).Value
Exit For
End If
Next i
Application.ScreenUpdating = True
End Function
SearchSheet.Select
您不能在称为 UDF 的函数中执行此操作。 有关详细信息,请参阅 https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel。
虽然您不需要 select sheet - 只需使用
FinalRow = SearchSheet.Cells(Rows.Count, 2).End(xlUp).Row
(和以下代码类似)