LoginSignup
0
0

More than 1 year has passed since last update.

AccessのVBAでエクセルファイル内に非表示シートが存在するか判定

Last updated at Posted at 2023-01-20

概要

Accessで外部エクセルファイルを操作する前に
『エクセルファイル内に非表示シートが存在するか』
を判定したい。

非表示シートがあると下記のような弊害がある為。

やり方

非表示シートが存在するエクセルファイルか判定
Public Function IsHideSheetExistExcelFile(ByVal excel_file_path As String) As Boolean


    Dim excel_ As New excel.Application


    'バックグラウンドで起動。
    excel_.Visible = False
    excel_.UserControl = False
    excel_.Workbooks.Open fileName:=excel_file_path
    

    '全シート走査して表示状態確認。
    'excel_.WorksheetsはFor Eachのインターフェース無いのでForでまわす必要あり。
    Dim i_ As Long
    For i_ = 1 To excel_.Worksheets.Count
        If Not excel_.Worksheets(i_).Visible Then
            IsHideSheetExistExcelFile = True
            'バックグラウンドのエクセルを閉じる必要がある為、早期リターンはしない。
        End If
    Next
    
    
    'バックグラウンド起動のエクセル終了。
    excel_.Workbooks(1).Close SaveChanges:=False
    excel_.Quit
    

End Function

蛇足

非表示シートが一つでもあれば処理させないように設計した方がいいかも。

エクセルは人が見て処理することが多い。
非表示シートがあるということはつまり、そこに記載された内容を見落とす危険がある。

参考サイトさん

バージョン

Windows 10 Pro 22H2 OSビルド 19045.2486
Microsoft Access for Microsoft 365 MSO (バージョン 2212 ビルド 16.0.15928.20196) 32 ビット

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0