vba实现文件内容汇总

1.功能介绍
    本功能主要实现将同一个文件夹的相似Excel文件里的内容汇总到同一个EXCEL文件中。

2.代码实现
Sub ボタン3_Click()
    Dim dstSheet As Worksheet
    Set dstSheet = ThisWorkbook.Worksheets(1)
    Const Path As String = "D:\ou\Calibration\Result\"
    Dim buf As String
    Dim value As String
    Dim temp() As String
     '入力箇所
    Const prefix = "result_"
    Const center_str = "Yaw"
    
    Const cnsDIR = "\" + prefix + center_str + "*.csv"
    buf = Dir(Path & cnsDIR, vbNormal)
    
    Dim i As Long
    Do While buf <> ""
        i = i + 1
        Dim srcBook As Workbook
        Set srcBook = Workbooks.Open(Path + buf)
        Dim srcSheet As Worksheet
        Set srcSheet = srcBook.Worksheets(1)
        dstSheet.Cells(10, i).value = srcSheet.Cells(1, 1)
        srcBook.Close False
        'ファイル名を分割
        temp = Split(buf, "_")
        value = Mid(temp(1), Len(center_str) + 1, Len(temp(1)) - Len(center_str) - 4)
        dstSheet.Cells(9, i).value = value
        buf = Dir()
       
    Loop

End Sub

你可能感兴趣的:(VBA)