“对象变量或With块变量未设置”的错误
Public Function FindSheet(sheetName As String) As Worksheet
Dim name As String
For I = 0 To ThisWorkbook.Sheets.Count - 1
name = ThisWorkbook.Sheets(I + 1).name
If name = sheetName Then
FindSheet = ThisWorkbook.Sheets(I)
Exit Function
End If
Next I
FindSheet = ThisWorkbook.Sheets(1) '运行到这一句时出现 “对象变量或With块变量未设置”的错误
End Function
[解决办法]
Public Function FindSheet(sheetName As String) As Worksheet Dim name As String For I = 0 To ThisWorkbook.Sheets.Count - 1 name = ThisWorkbook.Sheets(I + 1).name If name = sheetName Then Set FindSheet = ThisWorkbook.Sheets(I) Exit Function End If Next I Set FindSheet = ThisWorkbook.Sheets(1) '运行到这一句时出现 “对象变量或With块变量未设置”的错误End Function