VBA中的应用程序定义或对象定义的错误将parameter passing给函数

我试图将一个工作表variables作为parameter passing给一个函数。 这是代码

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 

有人可以告诉我在这里做错了什么…

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

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

相反,尝试replace最后一行:

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