我有以下子程序(在module10中)。
Sub varWorksheet(wksht As String)
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(wksht)
Set ws = Nothing
End Sub
我希望能够将此子例程作为对此类变量的引用传递,而不是必须在每个例程中明确声明它:
Set ws = module10.varWorksheet("Sheet1")
我收到了编译错误 - >预期的函数或变量。
答案 0 :(得分:1)
你应该使用这样的功能。
Function varWorksheet(wksht As String) As Worksheet
On Error Resume Next
Set varWorksheet = ThisWorkbook.Sheets(wksht)
End Function
如果工作表不存在,它将不返回任何内容。这很好。
Sub Test()
Dim ws As Worksheet
Set ws = Modul10.varWorksheet("Tabelle4")
If ws Is Nothing Then
Debug.Print "No worksheet"
Else
' what ever you want
End If
End Sub