• 【VBA】从批量excel文件中获取数据


    需求:从命名规则的批量data文件中提取固定单元格的值,并拷贝到另一个excel中,进行统计

    步骤:

    1、打开report文件,弹出对话框,开始

    2、依次打开命名规则的的data文件n

    3、获取固定单元格数据并赋值给report文件的sheet1的A列(data序号)和B列(data)

    4、关闭data文件

    5、返回循环

    6、结束

    代码文件:点击下载

    日期:2020-12-01 09:31:37

    Sub getvaluefromfile()
    '
    ' get RTC frequency from excel files
    '
    
    '
        Dim path As String
        Dim file As String
        Dim Formula As String
        Dim sheetname As String
        Dim cellname As String
        Dim cellnum As String
            
        Dim icount%
        
        Dim WB_origin As Workbook
        Dim sheet_origin As Excel.Worksheet
        Dim originname As String
        
        Dim WB_target As Workbook
        Dim sheet_target As Excel.Worksheet
        
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
            icount = 0
            originname = "2020" 'the name character of data files is 2020
            
            path = Application.ActiveWorkbook.path & ""  'get data files path
            file = Dir(path & "*.xls")  'get the first excel file name
            
            If InStr(file, originname) <> 0 Then 'if it is data file, then open it
                Set WB_origin = Workbooks.Open(path & file)
            Else
                MsgBox "Start to open report file automatically,OK?"
                Set WB_target = Workbooks.Open(path & file)
            End If
             
            Do Until file = ""
                If InStr(file, originname) <> 0 Then '
                    icount = icount + 1
                    Set WB_origin = CreateObject(path & file)
                    'Set sheet_origin = WB_origin.Worksheets(1)
                    sheetname = Mid(file, 1, 19)
                    
                    cellname = "B" & icount
                    cellnum = "A" & icount
                    WB_target.Sheets(1).Range(cellnum).value = icount  'fill in the number
                    WB_target.Sheets(1).Range(cellname).value = WB_origin.Sheets(sheetname).Range("E51").value 'fill in the RTC frequency
                    
                    Workbooks(file).Close SaveChanges:=False
                Else
                    If icount > 1 Then MsgBox "Not data file,jump?"
                End If
                
                file = Dir
            Loop
            MsgBox "Finished ! In total " & icount & " files"
            Application.ScreenUpdating = True
             
    
    End Sub
    /*生命如此美好。认真工作之余,不要忘了认真对待生活,认真对待身边人!*/
  • 相关阅读:
    求逆序对的解法
    关于宽搜BFS广度优先搜索的那点事
    大数乘法 poj2389
    二分求幂(快速求幂,二进制求幂)
    2n皇后问题
    poj2406 Power Strings (kmp 求最小循环字串)
    poj1050查找最大子矩阵和
    二叉树的建立和遍历
    已知二叉树前序和中序,求二叉树。
    c/c++连接mysql数据库设置及乱码问题(vs2013连接mysql数据库,使用Mysql API操作数据库)
  • 原文地址:https://www.cnblogs.com/isha2088/p/14065669.html
Copyright © 2020-2023  润新知