首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VBA >

“对象变量或With块变量未设置”的异常

2012-08-08 
“对象变量或With块变量未设置”的错误Public Function FindSheet(sheetName As String) As WorksheetDim na

“对象变量或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

[解决办法]

VB code
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 

热点排行