VBA中的应用程序定义或对象定义错误,将参数传递给函数

时间:2014-04-22 08:08:09

标签: excel vba excel-vba

我试图将工作表变量作为参数传递给函数。这是代码

Public varcol as integer

Private Sub CommandButton2_Click()
Dim wrk as WorkSheet
If Range("A1") = "a" then
Set wrk = ThisWorkbook.Sheets("S1")
Call fun(wrk)
End If
End Sub

Function fun(ByVal wrk As Worksheet) As Double

Dim b As Integer
Dim w As Double
w = wrk.Cells(b, varcol).Value                  'Line with the error
For b = 4 To 12

   c = watt - w

   If c < 1 Then

   ah = Sheets("wrk").Cells(b, varcol - 1)

   End If

Next b

End Function

有人能说出我在这里做错了什么......

1 个答案:

答案 0 :(得分:2)

在“fun”函数中,您声明wrk是一个工作表,但是然后在引号内使用它:

ah = Sheets("wrk").Cells(b, varcol - 1)

相反,尝试用以下代码替换最后一行:

ah = wrk.Cells(b, varcol - 1)