我正在创建我的自定义功能。我编写了代码并将其作为Sub进行了测试,并且效果很好。然后,我将其转换为可以普遍使用的功能。
我的函数只有一个输入,这是从Excel工作表中选择的单元格。我希望该函数返回一个具有字符串类型的输出。但是,它返回空。
将代码从Sub转换为Function时发生的更改:
这是完整的代码:
Function IbpBomLevel(Selected As Excel.Range) As String
Dim Wb As Workbook
Set Wb = Workbooks("Kitap.xlsx")
Windows("Kitap.xlsx").Activate
ProductID = SelectedCell
On Error GoTo T_Error
Set Ws = Wb.Worksheets("Production Source Header")
Sheets("Production Source Header").Select
Set MyRange = Range("B:C")
SourceID = Application.WorksheetFunction.VLookup(ProductID, MyRange, 2, False)
i = 1
T = 0
Z = 1
If IsEmpty(SourceID) = False Then
Do While (IsEmpty(SourceID) = False) And (T = 0)
BomLevel = Z
Windows("Kitap.xlsx").Activate
Set Ws = Wb.Worksheets("Production Source Header")
Sheets("Production Source Header").Select
Set MyRange = Range("B:C")
SourceID = Application.WorksheetFunction.VLookup(ProductID, MyRange, 2, False)
Set FoundCell = ActiveSheet.Range("C:C").Find(What:=SourceID)
If Not FoundCell Is Nothing Then
Index = FoundCell.Row
Location = Cells(Index, 1)
Product = Cells(Index, 2)
Else
End If
x = i
i = i + 1
Windows("Kitap.xlsx").Activate
Set Ws = Wb.Worksheets("Production Source Item")
Sheets("Production Source Item").Select
Set MyRange = Range("B:B")
SourceID2 = Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False)
Do While (IsEmpty(SourceID2) = False) And (i - x = 1)
Set MyRange = Range("B:B")
SourceID2 = Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False)
Set FoundCell = ActiveSheet.Range("B:B").Find(What:=SourceID2)
If Not FoundCell Is Nothing Then
Index2 = FoundCell.Row
Item = Cells(Index2, 1)
Windows("Kitap.xlsx").Activate
Set Ws = Wb.Worksheets("Production Source Header")
Sheets("Production Source Header").Select
Else
End If
Y = i
i = i + 1
Windows("Kitap.xlsx").Activate
Set Ws = Wb.Worksheets("Production Source Resource")
Sheets("Production Source Resource").Select
Set MyRange = Range("B:B")
SourceID3 = Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False)
Do While IsEmpty(SourceID3) = False And (i - Y = 1)
Set MyRange = Range("B:B")
SourceID3 = Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False)
Set FoundCell = ActiveSheet.Range("B:B").Find(What:=SourceID3)
If Not FoundCell Is Nothing Then
Index3 = FoundCell.Row
Resource = Cells(Index3, 1)
Windows("Kitap.xlsx").Activate
Set Ws = Wb.Worksheets("Production Source Header")
Sheets("Production Source Header").Select
Else
End If
i = i + 1
Loop
Loop
fullText = fullText & " Location: " & Location & " // Header: " & Product & " // Item: " & Item & " // Resource: " & Resource
Z = Z + 1
ProductID = Item
Set MyRange = Range("B:C")
SourceID = Application.WorksheetFunction.VLookup(ProductID, MyRange, 2, False)
T_Error:
If Err.Number = 1004 Then
On Error Resume Next
T = 1
If IsEmpty(fullText) = True Then
IbpBomLevel = MsgBox("No Data")
Else
IbpBomLevel = fullText
End If
Else
End If
Loop
Else
MsgBox ("Bom Missing")
End If
End Function
我该如何解决此问题?谢谢。
答案 0 :(得分:0)
VBA函数无法返回MessageBox。 因此,这不行:
IbpBomLevel = MsgBox("No Data")
ProductID = SelectedCell
是什么?如果您的意思是活动单元格,请写ActiveCell
。如果选择了多个单元,则VBA词为Selection
。
代码中还有一些其他流程。
On Error Resume Next
(尝试重写代码,而不使用此代码。)If Err.Number = 1004 Then
(尝试重写代码,而不使用此代码。).Select
和.Activate
-How to avoid using Select in Excel VBA的用法Set MyRange = Range("B:C")
,写Set MyRange = Worksheets("SomeNameHere").Range("B:C")
Option Explicit
避免使用SelectedCell
之类的未定义变量。