LoginSignup
1
3

More than 3 years have passed since last update.

ExcelVBAで指定フォルダ内のbasファイルをインポートする

Posted at

ExcelVBAでbasファイルをインポートする

前準備

VBA プロジェクト オブジェクト モデルへのアクセスを許可しておく。

実装

ThisWorkbookに下記の処理実装する。

Private Sub Workbook_Open()

    'ここで読み込みたいモジュールがあるフォルダを指定する
    importModules "\\xxxx" 
    importModules "\\yyyy" 

End Sub

'指定フォルダの*.basをインポートする
Private Sub importModules(ByVal dirName As String)

    Dim bas As String
    Dim cmp As Object

    bas = Dir(dirName)
    Do Until bas = "" 

        If bas Like "*.bas" Then

            For Each cmp In ThisWorkbook.VBProject.VBComponents
                If cmp.Name & ".bas" = bas Then
                    ThisWorkbook.VBProject.VBComponents.Remove cmp
                End If
            Next cmp

            ThisWorkbook.VBProject.VBComponents.Import dirName & bas
            bas = Dir

        End If

    Loop

End Sub
1
3
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
1
3