zoukankan      html  css  js  c++  java
  • [VBA]汇总多个工作簿的指定工作表到同一个工作簿的指定工作表中

    sub 汇总多个工作簿()

    Application.ScreenUpdating = False

    Dim wb As Workbook, f As String, l As String, n As String, m As String, j As Integer

        f = ThisWorkbook.Path & ""

        l = f & "*.xls"

        m = Dir(l)

        Do While m <> ""

            If m <> ThisWorkbook.Name Then

            n = f & m

            Workbooks.Open (n)

             With ThisWorkbook.activesheet

            .Range("b4:at34").ClearContents

            For i = 4 To .Range("a1").CurrentRegion.Rows.Count

            For j = 2 To .Range("a1").CurrentRegion.Columns.Count - 2 Step 3

            For Each wb In Workbooks

                If wb.Name <> ThisWorkbook.Name Then

                 aa = Left(wb.Name, InStrRev(wb.Name, ".") - 1)

                    If .Cells(2, j).Value = aa Then

                    .Cells(i, j) = Application.VLookup(.Cells(i, 1), wb.Worksheets(1).Range("a:b"), 2, 0)

                    .Cells(i, j + 1) = Application.VLookup(.Cells(i, 1), wb.Worksheets(1).Range("a:c"), 3, 0)

                        If VBA.IsNumeric(ThisWorkbook.activesheet.Cells(i, j + 1)) = False Then

                        ThisWorkbook.activesheet.Cells(i, j + 2) = 0

                        ElseIf ThisWorkbook.activesheet.Cells(i, j + 1) = 0 Then

                        ThisWorkbook.activesheet.Cells(i, j + 2) = 0

                        Else

                        ThisWorkbook.activesheet.Cells(i, j + 2) = ThisWorkbook.activesheet.Cells(i, j) / ThisWorkbook.activesheet.Cells(i, j + 1)

                        End If

                    End If

                End If

            Next

            Next

            Next

            End With

            End If

            m = Dir

        Loop

       For Each wb In Workbooks

        If wb.Name <> ThisWorkbook.Name Then

        wb.Close False

        End If

        Next

    Application.ScreenUpdating = True

    End Sub

    效果图:

    不足:

    调用excel本身的函数vlookup,数据量大的话,会导致运行速度慢,表格卡住的问题,后期优化,应用数组解决。

  • 相关阅读:
    让IT工作者过劳的13个坏习惯zz
    WPF Radio button的解决方案
    程序退出的各种方法,如何关闭多线程。
    文本框输入自动切换输入法问题
    在外部js文件中读取带母版页的子页当中控件的值
    Showwindow/FindWindow/PostMessage 转
    vb读取字节中的某一位
    C# 禁止windows程序重复运行的两种基本方法
    C# DllImport的用法(转)
    sqlserver存储过程中sql语句连接及datetime字段的处理
  • 原文地址:https://www.cnblogs.com/susuye/p/7169209.html
Copyright © 2011-2022 走看看