弄清楚变量的范围以使简单的函数正常工作
Figuring out the scope of variables to make a simple function work correctly
以下 VBA 例程不起作用,我不明白为什么...
我尝试在顶部设置一组 public 变量以使变量在函数之间移动,但它不起作用。
下面代码的最终结果应该使 getthename
具有 cow
的值
Sub test()
getthename = test2(test1("elephant"))
End Sub
Function test1(NewTitle As String) As String
If NewTitle = "elephant" Then
NewTitle = "horse"
Else
NewTitle = "pig"
End If
End Function
Function test2(NewTitle As String) As String
If NewTitle <> "horse" Then
NewTitle = "cow"
Else
NewTitle = "rabbit"
End If
End Function
你的函数实际上应该return一个值:
Sub test()
getthename = test2(test1("elephant"))
End Sub
Function test1(NewTitle As String) As String
If NewTitle = "elephant" Then
test1 = "horse"
Else
test1 = "pig"
End If
End Function
Function test2(NewTitle As String) As String
If NewTitle <> "horse" Then
test2 = "cow"
Else
test2 = "rabbit"
End If
End Function
以下 VBA 例程不起作用,我不明白为什么...
我尝试在顶部设置一组 public 变量以使变量在函数之间移动,但它不起作用。
下面代码的最终结果应该使 getthename
具有 cow
Sub test()
getthename = test2(test1("elephant"))
End Sub
Function test1(NewTitle As String) As String
If NewTitle = "elephant" Then
NewTitle = "horse"
Else
NewTitle = "pig"
End If
End Function
Function test2(NewTitle As String) As String
If NewTitle <> "horse" Then
NewTitle = "cow"
Else
NewTitle = "rabbit"
End If
End Function
你的函数实际上应该return一个值:
Sub test()
getthename = test2(test1("elephant"))
End Sub
Function test1(NewTitle As String) As String
If NewTitle = "elephant" Then
test1 = "horse"
Else
test1 = "pig"
End If
End Function
Function test2(NewTitle As String) As String
If NewTitle <> "horse" Then
test2 = "cow"
Else
test2 = "rabbit"
End If
End Function