0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vbaで同一フォルダ内のファイルから値を取得する方法

Posted at
Sub GetValuesFromExcelFiles()
    Dim folderPath As String
    Dim fileName As String
    Dim filePath As String
    Dim tableName As String
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim cellValue As String

    ' 対象のフォルダパスを指定してください(例: "C:\path\to\folder\")
    folderPath = "C:\path\to\folder\"
    
    ' フォルダ内のファイルを検索
    fileName = Dir(folderPath & "テーブル定義書_*.xlsx")
    
    ' ファイルが見つかるまでループ
    Do While fileName <> ""
        ' ファイルのフルパス
        filePath = folderPath & fileName
        
        ' ファイル名からテーブル名を抽出 (例: "テーブル定義書_テーブル名.xlsx" → "テーブル名")
        tableName = Mid(fileName, 8, Len(fileName) - 12)
        
        ' ファイルを開く
        Set wb = Workbooks.Open(filePath)
        
        ' 指定したシートが存在するか確認
        On Error Resume Next
        Set ws = wb.Sheets(tableName)
        On Error GoTo 0
        
        ' シートが存在する場合はセルA1の値を取得してポップアップで表示
        If Not ws Is Nothing Then
            cellValue = ws.Range("A1").Value
            MsgBox "ファイル: " & fileName & vbCrLf & "シート: " & tableName & vbCrLf & "セルA1の値: " & cellValue
        Else
            MsgBox "ファイル: " & fileName & vbCrLf & "シート: " & tableName & "が見つかりません。"
        End If
        
        ' ファイルを閉じる
        wb.Close False
        
        ' 次のファイルを取得
        fileName = Dir
    Loop
End Sub
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?