
VBA
Option Explicit
Sub フォルダ内エクセルファイル一括オープン()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim wb As Workbook, cnt As Long, wind As Window
Dim myFolder As Folder, myFile As File
Set myFolder = FSO.GetFolder(ThisWorkbook.Path)
On Error GoTo myError
'各ファイルの拡張子を確認してエクセルファイルを判別
For Each myFile In myFolder.Files
If myFile.Path Like "*.xls*" Then
cnt = 0
For Each wb In Workbooks
cnt = cnt + 1
'指定のファイルが既に開いていた場合
If myFile.Name = wb.Name Then
'このブックの場合は何もしない
If wb.Name = ThisWorkbook.Name Then
Exit For
'このブック以外の場合
Else
MsgBox myFile.Path & "は既に開いています", vbInformation
Exit For
End If
'最後のファイルまで確認してファイル名が"~$*"でなければブックを開く
ElseIf (cnt = Workbooks.Count) And (Not myFile.Name Like "~$*") Then
Workbooks.Open myFile
Exit For
End If
Next
End If
Next
'各ウインドウが最大化されていなければ最大化する
For Each wind In Windows
With Application
If Not .WindowState = xlMaximized Then .WindowState = xlMaximized
End With
Next
Set myFolder = Nothing
Set myFile = Nothing
ThisWorkbook.Close
Exit Sub
myError:
Debug.Print Err.Number & vbCrLf & Err.Description
Resume Next
End Sub