我学了一点VBA已经有几年了,我肯定生锈了。
我正在尝试在Sub中定义变量,然后从该Sub调用各种函数,一些函数将调用其他函数等等。
到目前为止,代码如下:
'AREAS
Public Sub Areas()
Dim h, btf, bbf, tw, ttf, tbf As Double
h = 300
btf = 150
bbf = 150
tw = 7.1
tbf = 10.7
ttf = 10.7
Call TotalArea(h, btf, bbf, tw, ttf, tbf)
Call Sum_of_Areas(h, btf, bbf, tw, ttf, tbf)
End Sub
Function TotalArea(h, btf, bbf, tw, ttf, tbf As Double) As Double
TotalArea = btf * ttf + bbf * tbf + (h - ttf - tbf) * tw
End Function
Function Area_tf(btf, ttf As Double) As Double
Area_tf = btf * ttf
End Function
Function Area_bf(bbf, tbf As Double) As Double
Area_bf = bbf * tbf
End Function
Function Area_w(h, ttf, tbf, tw As Double) As Double
Area_w = (h - ttf - tbf) * tw
End Function
Function Sum_of_Areas(h, btf, bbf, tw, ttf, tbf As Double) As Double
Sum_of_Areas = Area_tf + Area_bf + Area_w
End Function
第一位运行正常,即TotalArea函数,但另一位(Sum_of_Areas)只是给我错误。我试图找到适当的方法,以及我需要声明的变量和位置。我需要将哪些变量传递给函数。
谢谢!
答案 0 :(得分:1)
您必须使用sub和function的相同类型变量。 并且不要求功能。
Public Sub Areas()
Dim h As Double, btf As Double, bbf As Double, tw As Double, ttf As Double, tbf As Double
Dim Total As Double, mySum As Double
h = 300
btf = 150
bbf = 150
tw = 7.1
tbf = 10.7
ttf = 10.7
Total = TotalArea(h, btf, bbf, tw, ttf, tbf)
mySum = Sum_of_Areas(h, btf, bbf, tw, ttf, tbf)
Range("a1") = Total
Range("a2") = mySum
End Sub
Function TotalArea(h As Double, btf As Double, bbf As Double, tw As Double, ttf As Double, tbf As Double) As Double
TotalArea = btf * ttf + bbf * tbf + (h - ttf - tbf) * tw
End Function
Function Area_tf(btf As Double, ttf As Double) As Double
Area_tf = btf * ttf
End Function
Function Area_bf(bbf As Double, tbf As Double) As Double
Area_bf = bbf * tbf
End Function
Function Area_w(h, ttf, tbf, tw As Double) As Double
Area_w = (h - ttf - tbf) * tw
End Function
Function Sum_of_Areas(h As Double, btf As Double, bbf As Double, tw As Double, ttf As Double, tbf As Double) As Double
'Sum_of_Areas = Area_tf + Area_bf + Area_w
Sum_of_Areas = Area_tf(btf, ttf) + Area_bf(bbf, tbf) + Area_w(h, ttf, tbf, tw)
End Function