0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

エクセルファイルから値を取得する

Last updated at Posted at 2020-05-05
openFile.vba
Sub openFile()

    'ファイルパスの取得
    Dim strPath As String
    strPath = Sheets("Sheet1").Range("B2").Value 'C:/test/
    Debug.Print (strPath)
    
    '現ブックのシートを取得
    Dim workSheetData As Worksheet
    Set workSheetData = ThisWorkbook.Worksheets("Sheet1")

    'FileSystemオブジェクト変数の準備
    Dim objFSO As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'ファイル一覧を格納するオブジェクト変数
    Dim objFileList As Object
    Set objFileList = objFSO.GetFolder(strPath).files

    'ファイルを格納するオブジェクト変数
    Dim file As Object
    
    'ファイルパスの全ファイル
    For Each file In objFileList

        'ファイルを開く
        Dim wb As Workbook
        Set wb = Workbooks.Open(file, ReadOnly:=True)

        'シートのセルを取得
        Dim s As String
        s = wb.Worksheets("Sheet1").Cells(1, 1).Value
        's = Worksheets(1).Range("A1").Value
        's = wb.Worksheets(1).Cells(1, 1).Value
        Debug.Print (s)

        workSheetData.Cells(1, 1).Value = s

        '保存せずに閉じる
        Call wb.Close(SaveChanges:=False)

    Next file

End Sub



0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?