Function fncCheckSheetsExist(wbk As Workbook, sheetNames() As String) As String
' 変数の宣言
Dim i As Integer ' ループカウンタ
Dim sht As Worksheet ' ワークシートオブジェクト
Dim strMissingSheets As String ' 存在しないシート名のリスト
strMissingSheets = "" ' 初期化
' 各シート名の存在をチェック
For i = LBound(sheetNames) To UBound(sheetNames)
On Error Resume Next
Set sht = wbk.Sheets(sheetNames(i))
If sht Is Nothing Then
If strMissingSheets <> "" Then
strMissingSheets = strMissingSheets & ", "
End If
strMissingSheets = strMissingSheets & sheetNames(i)
End If
On Error GoTo 0
Set sht = Nothing ' オブジェクトの解放
Next i
fncCheckSheetsExist = strMissingSheets
End Function
コメント